HDU 1019 Least Common Multiple 数学例题

HDU 1019 Least Common Multiple 数学题解

求一组数据的最小公倍数。

先求公约数在求公倍数,利用公倍数,连续求所有数的公倍数就可以了。

#include <stdio.h>
int GCD(int a, int b)
{
	return b? GCD(b, a%b) : a;
}

inline int LCM(int a, int b)
{
	return a / GCD(a, b) * b;
}

int main()
{
	int T, m, a, b;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d %d", &m, &a);
		while (--m)
		{
			scanf("%d", &b);
			a = LCM(a, b);
		}
		printf("%d\n", a);
	}
	return 0;
}