先各自除以最大公约数,然后将两个结果和最大公约数相乘,即为最小公倍数。
输入样例:
6 5 18 12
输出样例:
30
36
#include<iostream>
#include<fstream>
using namespace std;
int gcd(int, int);
int main()
{
ifstream cin("test.txt");//向OJ提交时,注释此句
int m, n;
while (cin >> m >> n)
{
int tmp = gcd(m, n);
cout << m / tmp * n << endl;
}
system("pause");//向OJ提交时,注释此句
return 0;
}
int gcd(int m, int n)
{
int large, less;
large = m > n ? m : n;
less = n < m ? n : m;
while (less != 0)
{
int tmp = large % less;
large = less;
less = tmp;
}
return large;
}