解法一:------------------------------------------------------------------------------------------------------------
#include<iostream> using namespace std; int main(){ int a, b,c,d,m,n; cout << "请输入第1个数:"; cin >> a; cout << "请输入第2个数:"; cin >> b; m = a > b ? a : b; n = a > b ? b : a; c = m*n; d = m% n; while (d){ m = n; n = d; d = m%n; } c = c / n; cout << "这两个数的最大公约数为:" << n; cout << "" << endl; cout << "这两个数的最小公倍数为:" << c; cout << "" << endl; } 解法二:------------------------------------------------------------------------------------------------------------ #include<iostream> using namespace std; int gcd(int a, int b){ //返回a,b最大公约数 return (b>0) ? gcd(b, a%b) : a; //若b大于a,则递归gcd(b, a%b)时,a和b会互换 } int main(){ int a, b, c, d, m, n; cout << "请输入第1个数:"; cin >> a; cout << "请输入第2个数:"; cin >> b; c = a*b; n = gcd(a,b); c = c / n; cout << "这两个数的最大公约数为:" << n; cout << "" << endl; cout << "这两个数的最小公倍数为:" << c; cout << "" << endl; }