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;
}
评论