正文

求 n  个数的最小公倍数2007-09-05 18:58:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/29145.html

分享到:

Least Common Multiple
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Problem description
In arithmetic and number theory the least common multiple or lowest common multiple (lcm) or smallest common multiple of two integers a and b is the smallest positive integer that is a multiple of both a and b. If there is no such positive integer, e.g., if a = 0 or b = 0, then lcm(a, b) is defined to be zero.
For example, the least common multiple of the numbers 4 and 6 is 12.


Input
The input contains several cases. Each case has one line containing a non-negative integer N (N<=100), then followed N non-negative integers in the range of 0 and 2147483647. A ZERO terminates the input, which should not be processed.

Output
For each test case, output the least common multiple of the N integers in seperate lines. It is guaranteed that the lcm would not exceed 231 - 1.

Sample Input
2
12 18
5
3 9 27 81 243
0
Sample Output
36
243

//// 开始没考虑到 0 ,Run Time Error 一次,///// 水题 充数

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    int a[102],n,i;
    for(; cin>>n && n;){
        for(i=0; i<n; i++)
            cin>>a[i];
        sort(&a[0],&a[n]);
        if(a[0]==0){
            cout<<0<<endl;
            continue;
        }
        int small=a[n-1];
        for(i=n-2; i>=0; i--){
            if(small%a[i]){
                small+=a[n-1];
                i=n-1;
            }
        }
        cout<<small<<endl;
    }
    return 0;
}

阅读(5169) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册