第五次测试 多个数求最小公倍数

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
一组正整数的最小公倍数(LCM)是最小的正整数,它能被该集合中的所有数整除。例如,5的LCM。7和15等于105输入输入将由多个问题实例组成。输入的第一行将包含一个整数,表示问题实例的数量。每个实例将由表单mn1 n2 n3中的一行组成……其中m是集合中的整数个数,n1…nm是整数。所有整数都是正数,并且位于32位bit 的范围内。
输出
对于每个问题实例,输出一行包含相应LCM的代码。所有结果都位于32位bit 范围内。
很坑
坑的一批
这道题对数值进行了范围限制,所以如果你还是用int的变量提交肯定是错的,所以要用long long
贴上写的很烂的代码。

#include<stdio.h>
int  p(int a,int b){
	long long t;
	while(b!=0){
		t=a%b;
		a=b;
		b=t;
	}
	return a;
}
int main ()
{
	long long n,m;
	scanf("%lld",&n);
	while(n--){
		scanf("%lld",&m);
		long long a[m+5];
		long long sum;
		long long cut;
		long long t;
		long long i;
		long long s;
		for( i=0;i<m;i++){
			scanf("%lld",&a[i]);
		}
		if(m==1)
		{
			printf("%lld\n",a[0]);
		} else
		{		
			sum=p(a[0],a[1]);
			t=a[0]*a[1]/sum;
			for( i=2;i<m;i++){
				if(a[i]%t!=0){
					s=p(a[i],t);
					t=t*a[i]/s;
				}
			}
			printf("%lld\n",t);
	   }		
	}	
	return 0;
}

嗯,确实写的很烂,但是我已经懒得改了(wa了一晚上wa到哭)。
还有一个问题,在函数里面我们用的依旧是int,main里是long long,很奇怪它没有错。

猜你喜欢

转载自blog.csdn.net/weixin_43902655/article/details/84770267