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 <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #include <string> 10 #include <cstring> 11 #include <map> 12 #include <stack> 13 #include <set> 14 #include <numeric> 15 #include <bits/stdc++.h> 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/gcd(a,b)*b; //递归求最小公倍数 25 } 26 int main() 27 { 28 ll a,b; 29 while(cin >> a >> b) 30 { 31 cout << gcd(a,b)<< " "<<lcm(a,b)<<endl; 32 } 33 return 0; 34 }