杭电ACM 1019Problem DescriptionThe 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.InputInput will consist of multi
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/23 21:44:18
杭电ACM 1019Problem DescriptionThe 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.InputInput will consist of multi
杭电ACM 1019
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
#include
#include
#include
int gcd(int a,int b)
{
\x05int t=0;
\x05if(a
杭电ACM 1019Problem DescriptionThe 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.InputInput will consist of multi
首先max=b[0]应该加一个条件,就是说要在k=0的时候才行,否则的话,每次都要对他赋值,这个是没有必要的.(if(k==0) max=b[0];)
其次,你的算法有问题,最小公倍数不是两个数相乘就行了,而是能被两者除尽的最小的那个数.
代码如下:
我修改了你和回答人的代码,并且已经A了
#include "stdio.h"
long int LCM(int a,int b)//求最小公倍数的函数
{
long int x;
if (a>b)
{
x=a;
while (x%b!=0)
{ //小优化,既然x是a和b的最小公倍数,所以枚举时只要一直加a或b,再判断能否整除另一个数
x+=a;
}
return x;
}
else
{
x=b;
while (x%a!=0)
{
x+=b;
}
return x;
}
}
int main()
{
int m,n,i,j,k;
long int max,a[100],b[100];
scanf("%d\n",&m);
for(i=0;i<m;i++)
{
scanf("%d",&n);
for(j=0;j<n;j++)
scanf("%d",&a[j]);
for(k=1;k<n;k++)
{
a[k]=LCM(a[k],a[k-1]);//计算相连的两个数的最小公倍数,并替换
}
printf("%ld\n",a[k-1]);
}
return 0;
}