HDU 1019 Least Common Multiple
题意:输入t组样例,每组先输入一个n然后输入n个数,求者n个数的LCM(最小公倍数),其中每个数大小再int范围内
题解:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 每输入一个数就求其与前面数的lcm'的gcd,lcm = lcm'/gcd,以此来更新每一个lcm 3 */ 4 #include <iostream> 5 6 using namespace std; 7 8 int gcd (long long x, long long y) 9 { 10 if (x < y) swap(x, y); 11 while (y) { 12 int temp = y; 13 y = x % y; 14 x = temp; 15 } 16 return x; 17 } 18 19 int main() 20 { 21 int t, n, x; 22 cin >> t; 23 while (t--) { 24 cin >> n; 25 long long ans = 1; 26 while (n--) { 27 cin >> x; 28 int d = gcd(x, ans); 29 ans = (ans / d) * x; 30 } 31 cout << ans << endl; 32 } 33 return 0; 34 }