Problem Description
求n个数的最小公倍数。
Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。
Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。
Sample Input
2
4 6
3
2 5 7
Sample Output
12
70
#include<stdio.h>
#include<math.h>
void main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int max=0;
int a[100];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if (max<a[i])
max=a[i];
}
int flag=0;
for(int i=0;i<n;i++)
{
if(max%a[i]!=0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d\n",max);
else
{
int k=max;
while(k++)
{
int i;
for(i=0;i<n;i++)
{
if(k%a[i]!=0)
break;
}
if(i==n)
{
printf("%d\n",k);
break;
}
}
}
}
}
啊这题难的是思路算法。我想的是,把每个合数都拆成质数,然后拆出来的不重复的质数相乘。但是把每个合数拆成质数也不容易吼。←←这是我一开始的想法,还得判断是不是质数,不是质数就拆,拆出来的质数还得去重。贼麻烦。懒得写,后来还是去网上抄答案了。结果发现网上全是穷举法???我???可是我好怕麻烦,我的那个算法也没快到哪去,就决定穷举去了。