您的位置:首页 > 其它

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

2016-02-25 09:52 232 查看
#include <iostream>
using namespace std;
int main()
{
int i, j;
int temp;
cin >> i >> j;
if (i < j)
{
temp = i;
i = j;
j = temp;
}
int amass = i * j;
while (j != 0)
{
temp = i%j;
i = j;
j = temp;
}
cout << "最大公约数是: " << i << endl;
cout << "最小公倍数是: " << amass / i << endl;

system("pause");
return 0;
}


最大公约数的递归实现

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

int gcd(int x, int y);
int main()
{
int x=4, y=5;
int temp = 0;
if (x < y)
{
temp = x;
x = y;
y = temp;
}
cout << gcd(x, y);

system("pause");
return 0;
}

int gcd(int x, int y)
{
if (y==0)
{
return x;
}
else
{
return gcd(y, x%y);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息