码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1019 Least Common Multiple

时间:2014-08-30 08:43:49      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:style   io   for   sp   amp   on   size   c   as   

题解:求n个数的最小公倍数,一个一个算就可以了,需要注意的是LCM先除GCD再乘,因为先乘有可能会超范围,1WA的代价。

#include <cstdio>
int T,n,a,b;
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);a=1;
        for(int i=0;i<n;i++){
            scanf("%d",&b);
            a=a/gcd(a,b)*b;
        }printf("%d\n",a);
    }return 0;
}

HDU 1019 Least Common Multiple

标签:style   io   for   sp   amp   on   size   c   as   

原文地址:http://www.cnblogs.com/forever97/p/3946028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!