1062: Computer Versus Mankind
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 3s | 8192K | 2437 | 1119 | Standard |
Mike is a fan on programming. He's sure that computer can do everything faster than mankind does if a kind of task is repeated certain amount of times. Now he is proving this conclusion by calculate the gcd and lcm of two positive integers a and b, where a,b>1.
The gcd of two positive integers a and b, where a,b>1, is the greatest common dividor of them. The lcm of two positive integers a and b, where a,b>1, is the least common multiple of them. For example, the gcd of 6 and 8 is 2 while the lcm of them is 24.
Input Specification
The input consists of multilines, each line consists of two integers a and b(0<a,b<231), separated by a single space. a=b=0 marks the end of input, which you shouldn't process.
Output Specification
For each pair a and b in the input, you should output the gcd and lcm on a single line, separated by a single space too.
Sample Input
6 8 5 2 0 0
Sample Output
2 24 1 10
Problem Source: 1st JOJ Cup Online VContest Warmup Problem
/*
水题
最大公约数,最小公倍数
非递归版 gcd
*/
#include <stdio.h>
int m,n;
int gcd(int a,int b)
{
int tmp;
if(a < b)
{
tmp = a;
a = b;
b = tmp;
}
while(a % b != 0)
{
tmp = b;
b = a % b;
a = tmp;
}
return b;
}
int lcm(int a,int b)
{
return a * b / gcd(a,b);
}
int main()
{
while(scanf("%d %d",&m,&n),m != 0 && n != 0)
{
if(m == 0 || n == 0)
break;
printf("%d %d\n",gcd(m,n),lcm(m,n));
}
return 0;
}
本文介绍了一个简单的程序,用于计算两个正整数的最大公约数(GCD)和最小公倍数(LCM)。通过非递归的方式实现了GCD的计算,并利用GCD计算LCM。输入为多组数据,每组包含两个正整数,输出每组数据的GCD和LCM。
597

被折叠的 条评论
为什么被折叠?



