华为机试 HJ108题解 | #求最小公倍数#
求最小公倍数
https://www.nowcoder.com/practice/22948c2cad484e0291350abad86136c3
#include <iostream>
#include <cmath>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
// 求a和b的最小公倍数
// 先求a和b的最大公约数m
int GetMinBeiShu(int a, int b)
{
int minBeiShu;
int x = a * b;
// 求a和b的最大公约数
int yueShu = 1; // 默认为1
int min = std::min(a, b);
int max = std::max(a, b);
for (int n = 1; n <= max; n++) {
if (a % n == 0 && b % n == 0) {
yueShu = n;
}
}
return a * b / yueShu;
}
int main() {
int a, b;
while (cin >> a >> b) { // 注意 while 处理多个 case
cout << GetMinBeiShu(a, b) << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
查看8道真题和解析