GDUFE ACM-1144

题目:http://acm.gdufe.edu.cn/Problem/read/id/1144

 

GCD与LCM之和

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

 听说师弟师妹们最近学习c语言进度比较慢,小G决定出一道可以现学现用的,即与最大公约数(gcd)、最小公倍数(lcm)相关题目。
现有2个不超过100正整数 a 和 b,求gcd(a,b)+lcm(a,b);

Input:

第一行输入一个n,表示有n组测试数据,接下来的n行,每行输入两个正整数a,b。

Output:

对于每组输入数据,输出对应的答案,每组输出占一行。

Sample Input:

3
1 2
6 9
98 41

Sample Output:

3
21
4019

思路:求出最大公约数再求出最小公倍数然后加起来

难度:简单

代码:
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int n,a,b;
 5     int gcd(int a,int b);
 6     int lcm(int a,int b);
 7     while(~scanf("%d",&n))
 8     {
 9         while(n--)
10         {
11             scanf("%d %d",&a,&b);
12             printf("%d\n",gcd(a,b)+lcm(a,b));
13         }
14     }
15     return 0;
16 }
17 
18 int gcd(int a,int b)
19 {
20     int c,i;
21     if(a>b)
22     {
23         c=a;a=b;b=c;
24     }
25     for(i=0;;i++)
26     {
27         if(b%a==0) break;
28         c=b%a;
29         b=a;
30         a=c;
31     }
32     return a;
33 }
34 
35 int lcm(int a,int b)
36 {
37     int c;
38     c=a*b/gcd(a,b);
39     return c;
40 }

 

posted @ 2016-10-28 20:07  ruoruoruoruo  阅读(107)  评论(0编辑  收藏  举报