您的位置:首页 > 其它

hud 1019最小公倍数

2015-11-19 21:47 357 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019

思路:头两个数先求,再用所求的数与后面的一个数求,依次类推

#include<stdlib.h>
#include<time.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string.h>
using namespace std;

int n,m;
int arr[1000];

int gcd(int a,int b)
{
if(b)
return gcd(b,a%b);
else
return a;
}

int lcm(int a,int b)
{
return a/gcd(a,b)*b;  //此处不能用a*b/gcd(a,b)数据会溢出
}

int main()
{
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
scanf("%d",&m);
int t;
scanf("%d",&t);
for(int i=1;i<=m-1;i++)
{
scanf("%d",&arr[i]);
t=lcm(t,arr[i]);
}
printf("%d\n",t);

}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: