欧几里德算法,也就是那个辗转相除法求最大公约数,最小公倍数的算法,
就是要注意求最小公倍数的时候一定要先乘后除!
#include <stdio.h> #include <string.h> #include <iostream> #include <string> using namespace std; /* 辗转相除法-即欧几里德算法 */ int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); } int main() { int a, b; cout << "Input the two numbers : " << endl; while (cin >> a >> b) { int ans = gcd(a,b); cout << "_gcd(a, b) : " << ans << endl; cout << "_lcm(a, b) : " << a / ans * b << endl; cout << "Input the two numbers : " << endl; } system("pause"); return 0; }