HDU-2028Lowest Common Multiple Plus

Problem Description

求n个数的最小公倍数。

Input

输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。

Output

为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。

Sample Input

2 4 6

3 2 5 7

Sample Output

12

70

最小公倍数=a*b/最大公约数。

代码:

#include<stdio.h>

int gcd(int a,int b)
{
	int c;
	while(a%b)//辗转相除求最大公约数
	{
		c=a%b;
		a=b;
		b=c;
	}
	return b;
}
 
int gbd(int a,int b)
{
	return a/gcd(a,b)*b;//防溢出 求最小公倍数
}
 
int main()
{
	int n,i,s,a;
	while(scanf("%d",&n)!=EOF)
	{
		scanf("%d",&s);
		for(i=1;i<n;++i)
		{
			scanf("%d",&a);
			s=gbd(s,a);//每次求一次最小公倍数
		}
		printf("%d\n",s);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hello_cmy/article/details/81408453