HDU2028

作者: 费曼JW | 来源:发表于2019-04-18 21:33 被阅读0次

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2028

题目要求:
求n个数的最小公倍数。

思路:
如果为三个数x,y,z求最小公倍数,可先求x,y的最小公倍数t,再求t,y的最小公倍数即为所求。
以此类推...

代码:

#include <stdio.h>
int gcd(int a, int b);
int main()
{
    int n;
    while (scanf("%d", &n) != EOF)
    {
        int a,ans=1,i;
        for (i=0; i < n; i++)
        {
            scanf("%d", &a);
            ans = gcd(a,ans);
        }
        printf("%d\n", ans);
    }
}
int gcd(int a, int b)
{
    int temp, ans, c = a, d = b;
    if (c < d)
    {
        temp = c;c = d;d = temp;
    }
    while (d != 0)
    {
        temp = c % d;c = d;d = temp;   //求最大公约数
    }
    ans =( a / c) * b;   // 最小公倍数等于两数之积除以其最大公约数 && a*b可能超出int的范围
    return ans;
}

相关文章

  • HDU2028

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2028 题目要求:...

网友评论

      本文标题:HDU2028

      本文链接:https://www.haomeiwen.com/subject/wuyxgqtx.html