Lowest Common Multiple Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33702 Accepted Submission(s): 13766
Problem Description 求n個數的最小公倍數。
Input 輸入包含多個測試實例,每個測試實例的開始是一個正整數n,然後是n個正整數。
Output 為每組測試數據輸出它們的最小公倍數,每個測試實例的輸出占一行。你可以假設最後的輸出是一個32位的整數。
Sample Input2 4 6 3 2 5 7
Sample Output12 70
Author lcy
Source C語言程序設計練習(五)
Recommend lcy | We have carefully selected several similar problems for you: 2032 2031 2029 2030 2035
Statistic | Submit | Discuss | Note
正常的水題吧。
先用簡單的輾轉相除法求出最大公約數,再求出最小公倍數。
注意需要使用unsigned才可AC。
#includeusing namespace std; unsigned getcount(unsigned a, unsigned b){ unsigned c; unsigned m = a; unsigned n = b; if (a < b){ unsigned temp = a; a = b; b = temp; } while (b){ c = a%b; a = b; b = c; } return (m*n) / a; } int main(){ unsigned n, res, temp; while (cin >> n){ if (n > 0){ cin >> temp; res = temp; n--; } while (n--){ cin >> temp; res = getcount(res, temp); } cout << res << endl; } return 0; }