题目详情:
最小公倍数
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 94170 Accepted Submission(s): 50358
Problem Description 给定两个正整数,计算这两个数的最小公倍数。 Input 输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数. Output 对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。 Sample Input 10 14 Sample Output 70 Source POJ Recommend Eddy | We have carefully selected several similar problems for you: 1008 1021 1061 1005 1071
解题思路:
1、最小公倍数=两数的乘积/最大公约(因)数。
2、分解质因数法:先列出相关数的质因数,最小公倍数等于所有的质因数的乘积。
3、公式法:由于两个数的乘积,等于这两个数的最大公约数与最小公倍数的积,所以求最小公倍数需先求出最大公约数,用公式求出最小公倍数。
AC代码:
#include<iostream>
using namespace std;
int gcd(int a,int b){//a min,b max
if(b!=0) return gcd(b,a%b);
return a;
}
int main(){
int a,b;
while(cin>>a>>b){
cout<<a*b/gcd(a,b)<<endl;//利用公式法求解
}
system("pause");
return 0;
}