package chapterone;
import java.util.Scanner;
public class Exercise1_1_24 {
public static int gcd1(int num1, int num2) {//大数时出现 java.lang.StackOverflowError(instance: 1111111 1234567)
if (num1 == num2) {
return num1;
} else if (num1 > num2) {
return gcd1(num1 - num2, num2);
} else {
return gcd1(num1, num2 - num1);
}
}
public static int gcd2(int num1, int num2){//perfect
int max=(num1>num2)?num1:num2;
int min=(num1>num2)?num2:num1;
if(max%min==0){
return min;
}else{
return gcd2(max%min, min);
}
}
public static void main(String[] args) {
int num1, num2;
Scanner scan = new Scanner(System.in);
while (true) {
num1 = scan.nextInt();
num2 = scan.nextInt();
if (num1 == -1 && num2 == -1) {
break;
}
System.out.print("gcd1: "+gcd1(num1, num2)+" ");
System.out.println("gcd2: "+gcd2(num1, num2));
}
scan.close();
}
}
Exercise1_1_24
最新推荐文章于 2018-12-25 15:18:00 发布