Java 求最大公约数gcd

    import java.math.*;
    BigInteger a=new BigInteger("4") ;
    BigInteger b=new BigInteger("12") ;
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner cin=new Scanner(System.in);
        int a=cin.nextInt();
        int b=cin.nextInt();
        System.out.printf("%d\n",gcd(a,b));
        return;
    }
    public static int gcd(int a,int b)
    {
        return b==0? a:gcd(b,a%b);
    }

    public static BigInteger gcd(BigInteger a, BigInteger b)
    {
        return b.compareTo(BigInteger.ZERO) == 0 ? a : gcd(b, a.mod(b));
    }
}
posted @ 2021-08-10 10:04  斯文~  阅读(102)  评论(0)    收藏  举报

你好!