给定两个正整数,计算这两个数的最小公倍数。
Input
输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.
Output
对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。
Sample Input
10 14
Sample Output
70
对于这道题,其实是考一条数学概念,即A * B = 最大公约数 x 最小公倍数。
最大公约数可以通过辗转相除法,递归得到。而最小公倍数可由上公式得到。
C++代码如下:
#include <iostream>
#include <string>
using namespace std;
int f(int a,int b)
{
if(b==0)
return a;
else
return f(b,a%b);
}
int main()
{
int a ,b;
while(cin >> a >> b)
{
cout << a * b / f(a ,b) << endl;
}
}
来源:CSDN
作者:秋刀.
链接:https://blog.csdn.net/qq_44953321/article/details/103856480