AOJ 0003
問題
GCD and LCM
Write a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given a and b (0 < a, b ≤ 2,000,000,000). You can supporse that LCM(a, b) ≤ 2,000,000,000.
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.
Output
For each data set, print GCD and LCM separated by a single space in a line.
Sample Input
8 6
50000000 30000000Output for the Sample Input
2 24
10000000 150000000
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0005
解答
#include<iostream> using namespace std; int gcd(int a, int b){ if(a == 0 ){ return b; } else if ( b == 0){ return a; } else if (a >= b ){ a = a - b * ( a / b ) ; return gcd(a,b); } else if ( b > a){ b = b - a * ( b / a); return gcd(a,b); } } int main(){ signed int a, b; signed int g , l; while(cin >> a >> b){ g = gcd(a,b); l = a / g * b; cout << g << " " << l << endl; } return 0; }
非負数しか入力されないから unsigned intで (適当)
http://www5c.biglobe.ne.jp/~ecb/c/01_04.html