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

Posted hua-dong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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");
        }
    }
}

 

 

 

以上是关于CF1245 A. Good ol' Numbers Coloring(java与gcd)的主要内容,如果未能解决你的问题,请参考以下文章

CF1245F: Daniel and Spring Cleaning

CF1245E:Hyakugoku and Ladders

CF1245D: Shichikuji and Power Grid

CF1245D Shichikuji and Power Grid

CF1245D Shichikuji and Power Grid

CF1245F Daniel and Spring Cleaning(等会了更新)