分别用穷举法和欧几里德算法实现求两个整数的最大公约数
import java.util.Scanner;
public class Compare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int a = sc.nextInt();
System.out.println("请输入第二个整数:");
int b = sc.nextInt();
fuction1(a,b);
fuction2(a,b);
}
//穷举法
public static void fuction1(int a,int b) {
int t = 0;
for(int i = 1; i <= a; i++) {
if (a % i == 0 && b % i == 0)
t = i;
}
System.out.println("穷举法求得的最大公约数为:" + t);
}
//欧几里得算法
public static void fuction2(int a,int b) {
int c;
while(b != 0) {
c = a%b;
a = b;
b = c;
}
System.out.println("欧几里得算法求得的最大公约数为:" + a);
}
}