如何用c++简单实现从键盘输入两个整数,求出这两个整数的最小公倍数。
输入:8 12
输出:24
#include<iostream>
void main()
{
int a, b;
std::cin >> a >> b;
if (a < b) {//a小于b时交换
int temp = a;
a = b;
b = temp;
}
int greatestCommonDivisor=0;//最大公约数
int product = a * b;//乘积
while (a % b != 0) {//欧几里得算法求最大公约数
a = a % b;
b = b % a;
if (b)//若b不为0
greatestCommonDivisor = b;
else {
greatestCommonDivisor = a;
break;
}
}
int lowestCommonMultiple = product / greatestCommonDivisor;//最小公倍数=两数乘积/最大公约数
std::cout << lowestCommonMultiple;
}