CF1245 A. Good ol' Numbers Coloring(java與gcd)

題意:給定數字A和數字B,問是否知足gcd(A,B)==1。html

思路:能夠直接寫函數gcd。也能夠用大數自帶的gcd功能。java

代碼1:ide

    /*
    @author nimphy
    @create 2019-11-06-12:07
    about:
    */
    import java.io.*;
    import java.util.*;
    public class CF1245 {
        public static void main(String[] args) {
            Scanner In=new Scanner(System.in);
            int T,A,B,C;
            T=In.nextInt();
            while(T-->0){
                A=In.nextInt();
                B=In.nextInt();
                C=gcd(A,B);
                if(C==1) System.out.println("Finite");
                else System.out.println("Infinite");
            }
        }
        static int gcd(int x,int y)
        {
            int t;
            while(y>0){
                t=x%y;
                x=y;
                y=t;
            }
            return x;
        }
    }
View Code

 

代碼2:大數 ~ java有自帶進制轉化功能,能夠參考hdu5050函數

/*
@author nimphy
@create 2019-11-06-12:07
about:
*/

import java.math.*;
import java.io.*;
import java.util.*;

public class CF1245 {
    public static void main(String[] args) {
        Scanner In = new Scanner(System.in);
        int T;
        T = In.nextInt();
        while (T-- > 0) {
            String s1, s2;
            s1 = In.next();
            s2 = In.next();
            BigInteger A = new BigInteger(s1);//將轉換成十進制的數轉換成大數
            BigInteger B = new BigInteger(s2);
            BigInteger C = A.gcd(B);
            if (C.equals(BigInteger.ONE)) System.out.println("Finite");
            else System.out.println("Infinite");
        }
    }
}
相關文章
相關標籤/搜索