码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2028 Lowest Common Multiple Plus

时间:2015-09-06 01:13:52      阅读:355      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description

求n个数的最小公倍数。

Input

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

Output

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

Sample Input

2 4 6
3 2 5 7

Sample Output

12
70

思路:水题。题目要求32位整数,所以使用unsigned int。

代码:

#include <iostream>
#include <string.h>
#include <stack>

using namespace std;

unsigned int gcd(unsigned int a, unsigned int b)
{
    if (a == b)
    {
        return a;
    }
    if (a < b)
    {
        swap(a, b);
    }
    while (b != 0)
    {
        a %= b;
        swap(a, b);
    }
    return a;
}

int main()
{
    stack<unsigned int> s;
    int n;
    while (scanf("%d", &n) != EOF)
    {
        while (!s.empty())
        {
            s.pop();
        }
        while (n--)
        {
            unsigned int t;
            scanf("%d", &t);
            s.push(t);
        }
        while (s.size() > 1)
        {
            unsigned int a = s.top();
            s.pop();
            unsigned int b = s.top();
            s.pop();
            s.push(a*b / gcd(a, b));
        }
        printf("%d\n", s.top());
    }

    system("pause");
    return 0;
}


hdu 2028 Lowest Common Multiple Plus

标签:

原文地址:http://my.oschina.net/keyven/blog/501470

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!