import java.util.Scanner;
public class TestDemo4 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int c = a%b;
while(c != 0){
a = b;
b = c;
c = a % b;
}
System.out.println("最大公约数:"+b);
}
}
将两数a b求余得c:a % b = c;
若c = 0;则b为最大公约数;
若c != 0; 则a = b;b = c;从第一步开始继续执行,直至c = 0.
来源:CSDN
作者:yufy0528
链接:https://blog.csdn.net/yufy0528/article/details/104740368