HDU-2028 Lowest Common Multiple Plus

来源:互联网 发布:龙猫头像知乎 编辑:程序博客网 时间:2024/05/16 08:36

2016-07-06

HDU-2028 Lowest Common Multiple Plus

题目大意:求n个数的最小公倍数

解题思路:先求前两个数的最小公倍数,将前两数的最小公倍数与后面的数一一遍历求得最小公倍数。

#include <iostream>using namespace std;int main() {    int n;    int num[100];    int temp = 1;    int lcm;    int end;    while ( cin >> n ) {        for (int i = 0; i < n; i++) {            cin >> num[i];        }        while(1) {             if(temp % num[1] == 0 && temp % num[0] == 0)//先求前两个数的最小公倍数                                break;                        else                                 temp++;                }        lcm = temp;        for (int i = 2; i < n; i++) {//两两求最小公倍数            while(1) {                if(temp % num[i] == 0 && temp % lcm == 0)                    break;                else                    temp++;                }            lcm = temp;        }        cout << lcm << endl;        lcm = 1;        temp = 1;     }    return 0;}


1 0
原创粉丝点击