题解 | #求最小公倍数#

求最小公倍数

https://www.nowcoder.com/practice/22948c2cad484e0291350abad86136c3

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;


int maxG(int a, int b){
    int max0=1, zhenmax=1;
    int min0 =min(a, b);
    while (max0<=min0) {
        if (a%max0==0&&b%max0==0) {
            if (zhenmax<max0) {
                zhenmax=max0;
            }
        }
        max0++;
    }
    return zhenmax;
}

int main() {
    int a, b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        // cout << a + b << endl;
    }
    vector<int> ap;
    while (maxG(a, b)!=1) {
        ap.push_back(maxG(a,b));
        // cout<<ap.back()<<endl;
        a=a/ap.back();
        b=b/ap.back();
    }
    int output=1;
    for(auto x:ap){
        output=output*x;
    }
    cout<<output*a*b;

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务