package Basic;
import java.util.Scanner;
public class Gcd {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int num_1=scanner.nextInt();
int num_2=scanner.nextInt();
if(num_1>num_2){
System.out.println(gcd(num_1, num_2));
}
else {
System.out.println(gcd(num_2, num_1));
}
}
private static int gcd(int x,int y) {
int result = 0;
int temp = 0;
while(y!=0){
temp = y;
y=x%y;
x=temp;
}
return temp;
}
}
算法思路任意两个非零正整数,M,N求最大公约数,欧几里得算法采用的方法是重复应用下列等式,知道 M mod N =0;
gcd(m,n)=gcd(m mod n); m mod n表示 m%n
比如gcd(36,24)=gcd(24,12)=gcd(12,0)=12