shunfurh
编程介的小学生
采纳率92.7%
2017-06-01 02:22 浏览 856
已采纳

Lowest Common Multiple Plus

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

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

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

Sample Input
2 4 6
3 2 5 7

Sample Output
12
70

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答 默认 最新

  • 已采纳
    SuperKilling SuperKilling 2017-06-01 14:55

    Here is the program for your reference, however I have no time to test, just build seccessfully. Sorry for that you can test it by yourself.

    #include "stdafx.h"
    #include
    #include

    using namespace std;

    unsigned long int GreatestgCommonDivisor(unsigned long int a, unsigned long int b)
    {
    unsigned long int Max = 0, Min = 0, gcd = 0;
    Max = (a > b) ? a : b;
    Min = (a < b) ? a : b;

    gcd = Max;
    
    while (1)
    {
        if (gcd % Min == 0)
            break;
        gcd += Max;
    }
    return gcd;
    

    }

    int main()
    {
    unsigned int Number = 0, i = 0;
    unsigned long int data = 0, gcd = 0;

    std::vector<long int>               vecData, vecGCD;
    std::vector<long int>::iterator     itervecGCD;
    
    while (1)
    {
        cin >> Number;
        if (Number < 2)
        {
            cout << "there is not enough data" << endl;
            return   0;
        }
        for (i = 0; i < Number; i++)
        {
            cin >> data;
            vecData.push_back(data);
        }
        gcd = (vecData[0], vecData[1]);
        for (i = 2; i < Number; i++)
        {
            gcd = (gcd, vecData[i]);
        }
        vecGCD.push_back(gcd);
    }
    
    //Print output
    itervecGCD = vecGCD.begin();
    while (itervecGCD != vecGCD.end())
    {
        cout << *itervecGCD << "\n";
    }
    
    return 0;
    

    }

    点赞 评论 复制链接分享
  • SuperKilling SuperKilling 2017-06-02 04:43

    you can add any break condition in the while(1) to end of the input,
    and obviously the 2 include files are iotream and vector because "<>"can not display

    点赞 评论 复制链接分享
  • SuperKilling SuperKilling 2017-06-02 13:36

    Sorry I'm a littlle busy, so I have not test for you, just now I've fix some bugs and test through, you can end input with ctrl + z
    here is the program
    #include "stdafx.h"
    #include
    #include

    using namespace std;

    unsigned long int GreatestgCommonDivisor(unsigned long int a, unsigned long int b)
    {
    unsigned long int Max = 0, Min = 0, gcd = 0;
    Max = (a > b) ? a : b;
    Min = (a < b) ? a : b;

    gcd = Max;
    
    while (1)
    {
        if (gcd % Min == 0)
            break;
        gcd += Max;
    }
    return gcd;
    

    }

    int main()
    {
    unsigned int Number = 0, i = 0;
    unsigned long int data = 0, gcd = 0;

    std::vector<long int>               vecData, vecGCD;
    std::vector<long int>::iterator     itervecGCD;
    
    while (cin >> Number)
    {
        if (Number < 2)
        {
            cout << "there is not enough data" << endl;
            break;
        }
        vecData.clear();
        for (i = 0; i < Number; i++)
        {
            cin >> data;
            vecData.push_back(data);
        }
        gcd = GreatestgCommonDivisor(vecData[0], vecData[1]);
        for (i = 2; i < Number; i++)
        {
            gcd = GreatestgCommonDivisor(gcd, vecData[i]);
        }
        vecGCD.push_back(gcd);
    }
    
    //Print output
    itervecGCD = vecGCD.begin();
    while (itervecGCD != vecGCD.end())
    {
        cout << *itervecGCD << endl;
        itervecGCD++;
    }
    
    return 0;
    

    }

    点赞 评论 复制链接分享

相关推荐