C++求解最小公约数和最大公倍数

以下是代码实现:

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

// 最大公约数:greatest common divisor
int gcd(int a, int b) {
    
    
	return !b ? a : gcd(b, a % b);
}

// 最小公倍数:Least common divisor
int lcm(int a, int b) {
    
    
	return a * b / gcd(a, b);
}
int main() {
    
    
	cout << "greatest common divisor(gcd) = " << gcd(15, 25) << endl;
	cout << "Least common divisor(lcm) = "    << lcm(15, 25) << endl;
	return 0;
}

代码输出为:

greatest common divisor(gcd) = 5
Least common divisor(lcm) = 75

谢谢阅读

猜你喜欢

转载自blog.csdn.net/weixin_43869898/article/details/114272369
今日推荐