辗转相除法代码简化至9行
问题:给定两个数a,b,求它们的最大公因子
源代码:
public class Greater_Common_Divisor {
public static void main(String args[]) {
int a=9,b=15;
System.out.println(a+" , "+b+" -> "+compute(a,b));
}
static int compute(int a,int b) {
return a%b==0?b:compute(b,a%b);
}
}
运行结果: