求最大公约数和最小公倍数

img


不用辗转相除法怎么求最大公约数和最小公倍数

不用辗转相除法,那你就直接暴力遍历呗
公约数要从大往小遍历,公倍数要从小往大遍历

这篇除了 辗转相除法,还介绍了别的,参考下:

c++17引入了最大公约数和最小公倍数的算法


#include <iostream>
#include <numeric>
using namespace std;

int main()
 {
    // 最小公倍数
    cout << std::lcm(25, 20) << std::endl;
    // 最大公约数
    cout << std::gcd(60, 24) << std::endl;
    return 0;
}