hdu_2028_Lowest Common Multiple Plus

原题链接: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;
}

时间: 2024-11-07 00:01:14

hdu_2028_Lowest Common Multiple Plus的相关文章

hdu 1019 Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54584    Accepted Submission(s): 20824 Problem Description The least common multiple (LCM) of a set of positive integers is

题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)

题目链接:http://ac.jobdu.com/problem.php?pid=1439 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: // // 1439 Least Common Multiple.cpp // Jobdu // // Created by PengFei_Zheng on 10/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. /

ACM学习历程—HDU 3092 Least common multiple(数论 &amp;&amp; 动态规划 &amp;&amp; 大数)

hihoCoder挑战赛12 Description Partychen like to do mathematical problems. One day, when he was doing on a least common multiple(LCM) problem, he suddenly thought of a very interesting question: if given a number of S, and we divided S into some numbers

杭电 HDU ACM 2028 Lowest Common Multiple Plus

Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 39183    Accepted Submission(s): 16144 Problem Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Ou

HDUJ 1019 Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29480    Accepted Submission(s): 11136 Problem Description The least common multiple (LCM) of a set of positive integers is

Lowest Common Multiple Plus(杭电2028)

/*Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 33241    Accepted Submission(s): 13578 Problem Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数.

HDU 1019 Least Common Multiple (最小公倍数)

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30285    Accepted Submission(s): 11455 Problem Description The least common multiple (LCM) of a set of positive integers is

HDU 1019 Least Common Multiple 数学题解

求一组数据的最小公倍数. 先求公约数在求公倍数,利用公倍数,连续求所有数的公倍数就可以了. #include <stdio.h> int GCD(int a, int b) { return b? GCD(b, a%b) : a; } inline int LCM(int a, int b) { return a / GCD(a, b) * b; } int main() { int T, m, a, b; scanf("%d", &T); while (T--)

1019 Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 51959    Accepted Submission(s): 19706   Problem Description The least common multiple (LCM) of a set of positive integers is the smallest positiv