Created
April 14, 2015 15:18
-
-
Save sunocean/dcfe3d4c36f4874099a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //输入两个数,求最小公倍数和最大公约数 | |
| #include<stdio.h> | |
| int main(int argc, const char * argv[]) | |
| { | |
| int num1 = 0; | |
| int num2 = 0; | |
| int gcd = 0; //最大公约数 | |
| int lcm = 0; //最小公倍数 | |
| printf("请输入两个数:\n"); | |
| scanf("%d%d", &num1, &num2); | |
| int product = num1*num2; | |
| if (num1 > num2)//始终让num1是最小数。 | |
| { | |
| int temp = num1; | |
| num1 = num2; | |
| num2 = temp; | |
| } | |
| for (int i = num1; i > 0; i--) //从num1开始循环减少直到寻找到最大公约数 | |
| { | |
| if (num1%i == 0 && num2%i == 0) //能同时被 num1和 num2除尽的即为公约数 | |
| { | |
| gcd = i; | |
| break;//找到最大公约数后跳出循环 | |
| } | |
| } | |
| lcm = product/gcd;// 最大公约数和最小公倍数的关系 为 最大公约数 x 最小公倍数 = 两数相乘。 | |
| printf("gcd = %d\n", gcd); | |
| printf("lcm = %d\n", lcm); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment