码迷,mamicode.com
首页 > 其他好文 > 详细

杭电1019

时间:2015-04-29 21:10:35      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 37254    Accepted Submission(s): 14023


Problem Description
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105.

 

 

Input
Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 ... nm where m is the number of integers in the set and n1 ... nm are the integers. All integers will be positive and lie within the range of a 32-bit integer.
 

 

Output
For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.
 

 

Sample Input
2 3 5 7 15 6 4 10296 936 1287 792 1
 

 

Sample Output
105 10296
 
这题的大致意思是第一行输入有几个例子,后面几行首位是代表这一个例子有几个元素,然后求这几个元素的最小公倍数;
我用的方法是 最小公倍数=a*b-(a,b)的最大公倍数;先求出前两个数的最小公倍数,然后再拿这个公倍数跟后面几个去比较,依次下去;
最大公约数我用的是递归函数做的 具体方法是 辗转相除法;
下面是我的代码
 1 #include<stdio.h>
 2 __int64 gcd(__int64 n,__int64 m)//辗转相除法
 3 {
 4     if(m==0)
 5     return n;
 6     else
 7     return gcd(m,n%m);
 8 }
 9 int main()
10 {
11     int i,n,m;
12     __int64 a[100],min,max,t;
13     
14     scanf("%d",&n);
15     while(n--)
16     {
17         scanf("%d",&m);
18         for(i=0;i<m;i++)
19         {
20             scanf("%I64d",&a[i]);
21         }
22         
23             min=a[0];
24             for(i=1;i<m;i++)
25             {
26                 if(min<a[i])
27                 {
28                     t=min;
29                     min=a[i];
30                     a[i]=t;    
31                 }
32                 max=gcd(min,a[i]);
33                 min=min*a[i]/max;
34                 
35             }
36         printf("%I64d\n",min);    
37         
38     }
39 }

 

 

 

杭电1019

标签:

原文地址:http://www.cnblogs.com/yc721274287/p/4466874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!