HDU1019 Least Common Multiple

简介:
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1019
用辗转相除法求最小公倍数时要注意应该用a/y*b,若用a*b/y,则可能会数据溢出。
 #include<iostream>
#include <vector>
using namespace std;

int LCM(long x,long y)
{
    long temp; 
    long a,b; 
    a=x; 
    b=y; 
    while(x%y!=0) 
    { 
        temp=x%y; 
        x=y; 
        y=temp; 
    } 
    return a/y*b; 
}
void doProcess(vector<long>& v)
{
    if (v.size()==1)
    {//只有一个数据
        cout<<v[0]<<endl;
        return;
    }
    int temp = LCM(v[0],v[1]);
    for (int i=2;i<v.size();++i)
    {
        temp = LCM(temp,v[i]);
    }
    cout<<temp<<endl;
}
int main()
{
    int caseNum,i,j,n,tmp;
    while (cin>>caseNum)
    {
        for (i=0;i<caseNum;++i)
        {
            cin>>n;
            vector<long> numVect;
            for (j=1;j<=n;++j)
            {
                cin>>tmp;
                numVect.push_back(tmp);
            }
            doProcess(numVect);
        }
    }
    return 0;
}



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/01/04/1026431.html,如需转载请自行联系原作者
目录
相关文章
|
7月前
hdu 1019 Least Common Multiple
hdu 1019 Least Common Multiple
32 0
hdu 1019 Least Common Multiple
hdu 1019 Least Common Multiple
34 0
《Towards A Fault-Tolerant Speaker Verification System A Regularization Approach To Reduce The Condition Number》电子版地址
Towards A Fault-Tolerant Speaker Verification System: A Regularization Approach To Reduce The Condition Number
89 0
《Towards A Fault-Tolerant Speaker Verification System A Regularization Approach To Reduce The Condition Number》电子版地址
|
人工智能 BI
CF1169C. Increasing by Modulo(二分)
CF1169C. Increasing by Modulo(二分)
136 0
PAT (Advanced Level) Practice - 1143 Lowest Common Ancestor(30 分)
PAT (Advanced Level) Practice - 1143 Lowest Common Ancestor(30 分)
123 0
PAT (Advanced Level) Practice - 1068 Find More Coins(30 分)
PAT (Advanced Level) Practice - 1068 Find More Coins(30 分)
117 0
HDOJ1019Least Common Multiple
HDOJ1019Least Common Multiple
101 0
HDOJ 1019 Least Common Multiple(最小公倍数问题)
HDOJ 1019 Least Common Multiple(最小公倍数问题)
98 0
HDOJ 2028 Lowest Common Multiple Plus(n个数的最小公倍数)
HDOJ 2028 Lowest Common Multiple Plus(n个数的最小公倍数)
130 0