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

题意:给定数字A和数字B,问是否满足gcd(A,B)==1。

思路:可以直接写函数gcd。也可以用大数自带的gcd功能。

代码1:

    /*
    @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");
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/hua-dong/p/11804483.html