- GCD and LCM https://vjudge.net/problem/Aizu-0005
- 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 代码
- #include <iostream>
- #include <cstdio>
- #include <fstream>
- #include <algorithm>
- #include <cmath>
- #include <deque>
- #include <vector>
- #include <queue>
- #include <string>
- #include <cstring>
- #include <map>
- #include <stack>
- #include <set>
- #include <numeric>
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- ll gcd(ll a,ll b){
- if(b==0)
- return a;
- return gcd(b,a%b); // 递归求最大公约数
- }
- ll lcm(ll a,ll b){
- return a*b/gcd(a,b); // 递归求最小公倍数
- }
- int main()
- {
- ll a,b;
- while(cin>> a>> b)
- {
- cout << gcd(a,b)<< " "<<lcm(a,b)<<endl;
- }
- return 0;
- }
来源: http://www.bubuko.com/infodetail-3076284.html