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

hdu_2028_Lowest Common Multiple Plus

时间:2015-02-07 16:00:19      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2028
解题思路:求多个数的最小公倍数,先求第一、二个数的最小公倍数,再与第三个数求它的最小公倍数,重复此过程,第n个数则停止。即可输出结果。
<span style="white-space:pre">	</span>  注意两个32位的整数相乘会溢出。。。(不太明白数位溢出现象。。。)
#include <iostream>

using namespace std;

int lowCom(int a,int b)
{
    int t1 = a,t2 = b;
    while( a % b != 0 )
    {
        int temp = b ;
        b = a%b ;
        a = temp ;
    }
    return t1*(t2/b);//防止溢出 
}
int main()
{
    int n,a[10001];
    while(cin >> n)
    {
        memset(a,0,sizeof(a));
        for(int i = 1;i <= n;i++)
            cin >> a[i];
        int result;
  		if(n > 1) 
		  	result = lowCom(a[1],a[2]);
		  	
        for(int i = 3;i <= n;i++)
        {
            result = lowCom(result,a[i]);
        }
    	if(n == 1)
        	cout << a[n] << "\n";
    	else
			cout << result << "\n";
    }
    return 0;
}

hdu_2028_Lowest Common Multiple Plus

标签:

原文地址:http://blog.csdn.net/lyn12030706/article/details/43603489

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