[UPC](2783)Matrix Cypher ---- 矩阵初等变换

版权声明:本文为博主原创文章,转载请预先通知博主(〃'▽'〃)。 https://blog.csdn.net/m0_37624640/article/details/82946842

做法:

  • 感觉复习了一波线性代数~233,记录一下,防止忘记
  • 这个题,通过题目中给的两个矩阵,会发现,当是0字节的时候,它会把矩阵第二列中的值乘1加到第一列,第二列不变。
  • 当是1字节的时候,它会把矩阵第一列中的值乘1加到第二列,第一列不变。
  • 即这是最基本的矩阵初等列变换。
  • 我们每次判断第一列值的和 和第二列的和 谁大谁小,然后逆向模拟即可。

AC代码:

import java.lang.reflect.Array;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        BigInteger f[][] = new BigInteger[2][2];
        BigInteger a[][] = new BigInteger[2][2];
        BigInteger b[][] = new BigInteger[2][2];
        a[0][0] = BigInteger.ONE;
        a[0][1] = BigInteger.ZERO;
        a[1][0] = BigInteger.ONE;
        a[1][1] = BigInteger.ONE;
 
        b[0][0] = BigInteger.ONE;
        b[0][1] = BigInteger.ONE;
        b[1][0] = BigInteger.ZERO;
        b[1][1] = BigInteger.ONE;
        BigInteger e;
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                e = cin.nextBigInteger();
                f[i][j] = e;
            }
        }
        int ans[] = new int[150];
        int k = 0;
        while(true){
            if(f[0][0].add(f[1][0]) .compareTo(f[0][1].add(f[1][1])) == 0) break;
            if(f[0][0].add(f[1][0]) .compareTo(f[0][1].add(f[1][1])) > 0){
                ans[k] = 0;
                f[0][0] = f[0][0].subtract(f[0][1]);
                f[1][0] = f[1][0].subtract(f[1][1]);
                k++;
            }else if(f[0][0].add(f[1][0]) .compareTo(f[0][1].add(f[1][1])) < 0){
                ans[k] = 1;
                f[0][1] = f[0][1].subtract(f[0][0]);
                f[1][1] = f[1][1].subtract(f[1][0]);
                k++;
            }
        }
        for(int i=k-1;i>=0;i--) System.out.print(ans[i]);
        System.out.println();
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37624640/article/details/82946842