package cn.com.test3;
import java.util.Scanner;
/*
* 使用更相减损术求最大公约数
* 第一步:任意给定两个正整数;判断它们是否都是偶数。若是,则用2约简;若不是则执行第二步。
* 第二步:以较大的数减较小的数,接着把所得的差与较小的数比较,并以大数减小数。继续这个操作,直到所得的减数和差相等为止。
* 则第一步中约掉的若干个2与第二步中等数的乘积就是所求的最大公约数。
* */
public class test6 {
public static void main(String[] args) {
int x ,y ,z ;
System.out.println("输入两个int值");
Scanner scanner = new Scanner(System.in);
x = scanner.nextInt();
y = scanner.nextInt();
if (x < y) {
z = y ;
y = x ;
x = z ;
}
System.out.println(x +" "+ y);
int count = 0 ;
while (x % 2 == 0 && y % 2 == 0) {
x = x / 2 ;
y = y / 2 ;
count ++ ;
}
boolean bool = true ;
while (bool) {
x = x - y ;
if (x < y) {
z = y ;
y = x ;
x = z ;
}else if(x == y) {
bool = false ;
}
}
System.out.println("最大公约数为:"+x*Math.pow(2, count));
}
}