首页 > 技术文章 > 最大公约数 最小公倍数

zcy19990813 2018-09-15 14:01 原文

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

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\n",__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\n",a/d*b);
    }
    return 0;
}

 

推荐阅读