/**
* 求最大公约数
* */
public class GetGCD {
public static int gcd(int a, int b) {
if(a == b) {
return a;
}
if ((a & 1) == 0 && (b & 1) == 0){
return gcd(a >> 1, b >> 1) << 1;
}
else if((a & 1) == 0 && (b & 1) != 0){
return gcd(a >> 1, b);
}
else if ((a & 1) != 0 && (b & 1) == 0){
return gcd(a , b >> 1);
}
else {
int big = a > b ? a : b;
int small = a < b ? a : b;
return gcd(big-small, small);
}
}
public static void main(String [] args) {
System.out.println(gcd(4,8));
}
}
求最大公约数(Java)
猜你喜欢
转载自blog.csdn.net/qq_44837912/article/details/104147702
今日推荐
周排行