Lowest Common Multiple Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24414 Accepted Submission(s): 9870
Total Submission(s): 24414 Accepted Submission(s): 9870
Problem Description
求n个数的最小公倍数。
Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。
Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。
Sample Input
2 4 6 3 2 5 7
Sample Output
12 70#include <stdio.h> int gcd(int a,int b) { return b==0?a:gcd(b,a%b);//注意一下溢出的问题就行了,可怜我因为这个搞了好久(╯﹏╰)b } int lcm(int a,int b) { return a/gcd(a,b)*b; } main() { int a[100],n; while(scanf("%d",&n)==1) { int i,y=1; for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++){ a[i+1]=lcm(a[i],a[i+1]); } printf("%d\n",a[i]); } }