hdu2028 Lowest Common Multiple Plus

来源:互联网 发布:手机有算量软件吗 编辑:程序博客网 时间:2024/05/18 01:31

Lowest Common Multiple Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 61834    Accepted Submission(s): 25733


Problem Description
求n个数的最小公倍数。
 

Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。
 

Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。
 

Sample Input
2 4 63 2 5 7
 

Sample Output
1270
 

Author
lcy
 

欧几里得算法(迭代)

#include<iostream>using namespace std;int func(int m,int n) {    int t;    int oldm = m;    int oldn = n;        while(t = m % n) {       m = n;       n = t;    }    return (int)(oldm / n * oldn);}int main(void) {    int n;        while(cin >> n) {        int a[100];        for(int i = 0;i < n;i++) {            cin >> a[i];        }        int res = 1;        for(int i = 0;i < n;i++) {            res = func(a[i],res);        }        cout << res << endl;    }        return 0;}


(递归)

typedef long long ll;ll gcd(ll p,ll q) {    return q == 0 ? p : gcd(p,p%q) ;}lcm = p / gcd(p,q) * q;//防止超出数据类型







原创粉丝点击