最大公约数和最小公倍数(c++)
辗转相除法
#include<iostream>
using namespace std;
int main()
{
int m, n;
cin >> m >> n;
int a, b;
if (m > n) {
a = m;
b = n;
}
else {
a = n;
b = m;
}
while (a != b) {
a -= b;
if (a < b) {
int tmp = a;
a = b;
b = tmp;
}
}
cout << a << " " << m*n/a << endl;
return 0;
}
登录后发布评论
暂无评论,来抢沙发