HDU 2028 计算n个数的最小公倍数

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

求n个数的最小公倍数。

Input

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

Output

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

Sample Input

2 4 6
3 2 5 7

Sample Output

12
70

思路:两个数的最小公倍数=两个数的乘积/两个数的最大公约数。开long long 防止中间爆掉。

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a*b/gcd(a,b);
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        ll t1,t2;
        scanf("%lld",&t1);
        for(int i=0;i<n-1;i++)
        {
            scanf("%lld",&t2);
            t1=lcm(t1,t2);
        }
        printf("%lld\n",t1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/86662292
今日推荐