【Aizu - 0005 】GCD and LCM

【Aizu - 0005 】GCD and LCM

GCD and LCM


Descriptions:

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

题目链接:

https://vjudge.net/problem/Aizu-0005

 多组输入,就是求这两个数的gcd(最大公约数)和lcm(最小公倍数)

注意数据有点大,保险起见用long long吧

AC代码

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #include 
 6 #include 
 7 #include 
 8 #include 
 9 #include <string>
10 #include 
11 #include 
12 #include 
13 #include <set>
14 #include 
15 #include 
16 using namespace std;
17 typedef long long ll;
18 ll gcd(ll a,ll b){
19     if(b==0)
20         return a;
21     return gcd(b,a%b);  //递归求最大公约数
22 }
23 ll lcm(ll a,ll b){
24     return a*b/gcd(a,b);    //递归求最小公倍数
25 }
26 int main()
27 {
28     ll a,b;
29     while(cin >> a >> b)
30     {
31         cout << gcd(a,b)<< " "<endl;
32     }
33     return 0;
34 }

 

posted on 2019-05-29 22:13 Sky丨Star 阅读(...) 评论(...) 编辑 收藏

你可能感兴趣的:(【Aizu - 0005 】GCD and LCM)