HDOJ-1019 Least Common Multiple

http://acm.hdu.edu.cn/showproblem.php?pid=1019

题意:给出n个数,求它们的最小公倍数

 

对于n个数,它们的最小公倍数等于【前n-1个数的最小公倍数和第n个数】的最小公倍数

而前n-1个数的最小公倍数又等于【前n-2个数的最小公倍数和第n-1个数】的最小公倍数

以此类推..

所以,从第1和第2个数开始依次求出最小公倍数,然后迭代即可

 

# include <stdio.h>

int a, n, Result;

int Gcd(int x, int y)
{
	if(y == 0)
		return x;
	return Gcd(y, x%y);
}

int Lcm(int x, int y)
{
	int t = Gcd(x, y);
	return (x / t) * (y / t) * t;
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		scanf("%d",&Result);
		for(int i = 2; i <= n; i++)
		{
			scanf("%d",&a);
			Result = Lcm(Result, a);
		}
		printf("%d\n",Result);
	}
	return 0;
}

  

posted @ 2015-04-28 20:00  懒人の猫  阅读(154)  评论(0编辑  收藏  举报