我写的“求最大公约数和最小公倍数“,拿出来晾晾,高手指点.............
#include <stdio.h>int get_gy_gb(int num1, int num2) {
int i, min, result = 0;
min = (num1 > num2) ? num2 : num1; //Get the min num of the two.
for (i = 1; i <= min; i++) {
if (num1 % i == 0 && num2 % i == 0) {
result = (result >= i) ? result : i; //Get the max common divisor.
}
}
return result;
}
int main(void) {
int n1, n2, result;
printf("Please input two numbers: \n");
scanf("%d%d", &n1, &n2);
result = get_gy_gb(n1, n2);
printf("The max common divisor is : %d \n", result);
printf("The min common multiple is : %d \n", (n1 * n2) / result);
return 0;
}
有什么不足请高手们不吝赐教,谢谢!!!!!!!!!!!!
[[it] 本帖最后由 mqh21364 于 2008-3-12 17:49 编辑 [/it]]