公倍数与公约数

#include<stdio.h>
#include <iostream>
#include<math.h>

using namespace std;

//公约数
int getCommoDivisor(int x, int y)
{
    int k;
    if (x < y)
    {
        k = x;
        x = y;
        y = k;
    }
    if (x%y == 0)
        return abs(y);
    else
        return getCommoDivisor(y, x%y);
}

//公倍数
int getCommonMultiple(int x, int y)
{
    int z = getCommoDivisor(x, y);
    int t = (x*y) / z;
    return t;
}

int _tmain(int argc, _TCHAR* argv[])
{
    do
    {
        int x, y, z, t;
        cout << "请输入x=" << endl;
        cin >> x;
        cout << "请输入y=" << endl;
        cin >> y;
        z = getCommoDivisor(x, y);
        t = getCommonMultiple(x, y);
        cout << x << "" << y << "的最小公倍数为:" << t << endl;
        cout << x << "" << y << "的最大公约数为:" << z << endl;

        //getchar();

        system("pause");

        system("cls");
        
    } while (1);

    return 0;
}

 

posted @ 2018-02-09 11:50  poisson_notes  阅读(254)  评论(0编辑  收藏  举报