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

来源:互联网 发布:刺马案真相知乎 编辑:程序博客网 时间:2024/05/01 18:17
#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);    }}
0 0
原创粉丝点击