标签:
题目:HDU1019 Least Common Multiple
题目分析:就是最小公倍数和最大公约数的求解。
#include <iostream> #include <cstdio> using namespace std; int a[1005]; int GCD(int a, int b) { int temp; if (a<b) { temp=a; a=b; b=temp; } if (b==0) { return a; } else { return GCD(b,a%b); } } int LCM(int a, int b) { return (a*(b/GCD(a,b))); } int main() { int i,times,numbers,results; cin>>times; while (times--) { cin>>numbers; for (i=0;i<numbers;i++) { scanf("%d",&a[i]); } if (numbers==1) { cout<<a[0]<<endl; } else { results=LCM(a[0],a[1]); for (i=2;i<numbers;i++) { results=LCM(results,a[i]); } cout<<results<<endl; } } return 0; }
标签:
原文地址:http://www.cnblogs.com/weekend/p/5497721.html