GCD and LCM Aizu - 0005

題目:

Write a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given a and b.

Input

Input consists of several data sets. Each data set contains a and b separated by a single space in a line. The input terminates with EOF.

Constraints

  • 0 < a, b ≤ 2,000,000,000
  • LCM(a, b) ≤ 2,000,000,000
  • The number of data sets ≤ 50

Output

For each data set, print GCD and LCM separated by a single space in a line.

Sample Input

8 6
50000000 30000000

Output for the Sample Input

2 24
10000000 150000000

題意:

給你兩個數字a和b,計算a和b的最小公約數和最大公倍數;

代碼如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

typedef long long LL;

LL GCD(LL a,LL b)
{
    if(b==0)
        return a;
    else
        return GCD(b,a%b);
}

LL LCM(LL a,LL b)
{
    return a/GCD(a,b)*b;
}

int main()
{
    LL a,b;
    while(~scanf("%lld%lld",&a,&b))
    {
        long long int gcd=GCD(a,b);
        long long int lcm=LCM(a,b);
        printf("%lld %lld\n",gcd,lcm);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章