最大公约数
int divisor(int a, int b) {
int temp;
//比较两个数的大小,值大的数为a,值小的数为b
if (a < b) {
temp = a;
a = b;
b = temp;
}
//求余
while (b != 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
//函数递归求最大公约数
int gcd(int a, int b) {
if (a % b == 0)
return b;
else
return gcd(b, a % b);
}
最小公倍数
//辗转相除法求最大公约数函数
int divisor(int a, int b) {
int temp;
//比较两个数的大小,值大的数为a,值小的数为b
if (a < b) {
temp = a;
a = b;
b = temp;
}
//求余
while (b != 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
//求最小公倍数
int multiple(int a, int b) {
int divisor(int a, int b);
int temp;
temp = divisor(a, b);
return(a * b / temp);
}
性质:最大公约数和最小公倍数的乘积就是原两个数的积