zoukankan      html  css  js  c++  java
  • 最大公约数 最小公倍数

    求两个正整数的最大公约数。

    Input

    输入数据含有不多于50对的数据,每对数据由两个正整数(0 < n1,n2 < 232)组成。

    Output

    对于每组数据n1和n1,计算最大公约数,每个计算结果应占单独一行。

    Sample Input

    6 5 18 12

    Sample Output

    1
    6
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    int main()
    {
        long long a,b;
        while(~scanf("%lld%lld",&a,&b))
        {
            printf("%lld
    ",__gcd(a,b));
        }
        return 0;
    }

    求两个正整数的最小公倍数

    Input

    输入数据有多组测试数据,每对数据由两个正整数(0<n1,n2<100000)组成。

    Output

    对于每组数据n1和n1,计算最小公倍数,每个计算结果应占单独一行。

    Sample Input

    6 5 18 12

    Sample Output

    30 
    36

    Hint

    此题用int类型即可

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    int main()
    {
        long long a,b;
        while(~scanf("%lld%lld",&a,&b))
        {
            ll d=__gcd(a,b);
            printf("%lld
    ",a/d*b);
        }
        return 0;
    }
  • 相关阅读:
    腾讯安全上海游戏部门笔试题
    2017
    2016
    2015
    2014
    2013
    2012
    2011
    2010
    2009
  • 原文地址:https://www.cnblogs.com/zcy19990813/p/9702694.html
Copyright © 2011-2022 走看看