阿基里德.亚历山大
Euclid's algorithm finds the greatest common divisor by repeatedly subtracting the smaller number from the larger number until zero is reached. The number remaining is the greatest common divisor.
input : x1,x2
output: d
aigorithm prodess:
// Euclid's algorithm finds the greatest common divisor by repeatedly
// subtracting the smaller number from the larger number until zero
// reached. The number remaining is the greatest common divisor.
int euclid(int x, int y)
{
int t;
do {
if (x<y) {
t=x;
x=y;
y=t;
}
x -= y;
} while (x);
return y;
}
寻找2个整数的最大公约数
输入:x,y
1、判断:x<y
若x<y, 则t=x, x=y,y=x, x=x-y
若x>=y, 则x=x-y
2、判断:x==0,若x<=0,则返回y,否则返回步骤1