2020大一寒假培训(GCD LCM)

OJ题目

1 求最大公约数
Problem:203
Time Limit:1000ms
Memory Limit:65536K
Description
现在上课
给你2个正整数a和b (0< a,b<=1000),求他们的最大公约数。
今天不是递归特训么,怎么整起数论来了?
别急,今天借此机会介绍一种简单的算法来求最大公约数,即欧几里德算法(辗转相除法)
将两数求余,然后把余数与较小的数求余,直到余数为0时,就可得到最大公约数。
以36和20为例
36%20=16 20%16=4 16%4=0 由此得到36与20的最大公约数为4。
利用递归算法可以简洁得写出欧几里德算法的函数。
大家赶快动手,写出这个一劳永逸的模板吧
Input
输入的数据有多组,包含两个正整数a和b (0< a,b<=1000)。
Output
对于每组测试数据,输出两个整数a和b的最大公约数,每组输出占一行。
Sample Input
12 24
18 24
Sample Output
12
6
Hint
Source

#include <bits/stdc++.h>

using namespace std;
int gcd(int a,int b)
{
    if(b==0) return a;
    else{
            return gcd(b,a%b);
           }
}
int main()
{
    int a,b,ans;
    while(cin>>a>>b)
    {
        ans=0;
        ans=gcd(a,b);
         cout<<ans<<endl;
    }

    return 0;
}

2 最大公约数(求多个数的最大公约数)
Problem:581
Time Limit:1000ms
Memory Limit:65536K
Description
李华同学在大家的帮助下顺利的完成了第一题。但是第二题又把李华同学难住了……(李华到底会什么)
第二题的要求是求N(N <= 10)个数的最大公约数
Input
有多组测试用例,每组用例包括两行,第一行是一个整数N(2<=N<=10),第二行有N个数ai(1<= ai <= 100000)
Output
每组用例输出只有一行,包括一个整数,即这N个数的最大公约数
Sample Input
3
2 3 5
5
12 15 30 18 9
Sample Output
1
3
Hint
Source

#include <bits/stdc++.h>

using namespace std;
int gcd(int a,int b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}
int main()
{
    int n,a[100000];
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        int ans=0;
        for(int i=1;i<n;i++)
        {
            a[i+1]=gcd(a[i],a[i+1]);

        }
      cout<<a[n]<<endl;

    }
    return 0;
}

3 最大公约数 一样的水题,把int换long long
Problem:762
Time Limit:1000ms
Memory Limit:65536K
Description
给定两个正整数a和b,你的任务就是求它们的最大公约数,a和b的范围均在long long内。
Input
输入数据有多组,每组1行,分别是a和b。
Output
输出一个数,即a和b的最大公约数。
Sample Input
12 4
30 12
Sample Output
4
6
Hint
Source

#include <bits/stdc++.h>

using namespace std;
int gcd(int a,int b)
{
    if(b==0) return a;
    else{
            return gcd(b,a%b);
           }
}
int main()
{
    long long a,b,ans;
    while(cin>>a>>b)
    {
        ans=0;
        ans=gcd(a,b);
        cout<<ans<<endl;
    }

    return 0;
}

4 多个数的最大公约数
Problem:764
Time Limit:1000ms
Memory Limit:65536K
Description
给定n(n<=10)个正整数,你的任务就是求它们的最大公约数,所有数据的范围均在long long内。
Input
输入数据有多组,每组2行,第一行为n,表示要输入数字的个数,接下来第二行有n个正整数。
Output
输出一个数,即这n个数的最大公约数。
Sample Input
5
2 4 6 8 10
2
13 26
Sample Output
2
13
Hint
Source

#include <iostream>

using namespace std;
int gcd(long long a,long long b)
{
    if(b==0)return a;
    else return gcd(b,a%b);
}
int main()
{
    long long n,a[100000];
    while(cin>>n)
    {
          for(long long i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        long long ans=0;
        for(long long i=1;i<n;i++)
        {
            a[i+1]=gcd(a[i],a[i+1]);

        }
      cout<<a[n]<<endl;

    }
    return 0;
}

5 最大公约数和最小公倍数 (最小公倍数就等于两数乘积除于最大公约数)
Problem:1077
Time Limit:1000ms
Memory Limit:65536K
Description
请计算2个数的最大公约数和最小公倍数;(最大公约数可以使用辗转相除法,最小公倍数=2个数的乘积/它们的最大公约数;)
Input
输入数据有多组,每组2个正整数a,b(2<a,b<1000)
Output
在一行内输出a和b的最大公约数和最小公倍数;
Sample Input
15 10
Sample Output
5 30
Hint
Source

#include <bits/stdc++.h>

using namespace std;
int gcd(int a,int b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}
int main()
{
    int a,b;
    while(cin>>a>>b)
    {
        int ans=0,ans1=0;
        ans=gcd(a,b);
        ans1=(a*b)/ans;
        cout<<ans<<" "<<ans1<<endl;
    }
    return 0;
}

6 最大公约数-离散数学 (多个判断)
Problem:1100
Time Limit:1000ms
Memory Limit:65536K
Description
已知用辗转相除法可以计算2个数的最大公约数,2个数互素的条件是这2个数的最大公因子是1,现在的问题是让你判断2个数是否是互素的?(10分)
Input
输入数据有多组,每组有2个正整数a,b(1<=a,b<=1000000)
Output
如果这2个数互素,在一行内输出yes,否则输出no.
Sample Input
10 11
10 16
Sample Output
yes
no
Hint
gcd代码:int gcd(int a,int b)
{
if (b==0) return a;
else return gcd(b,a%b);

}
Source

#include <bits/stdc++.h>

using namespace std;
int gcd(int a,int b)
{
    if(b==0) return a;
    else{
            return gcd(b,a%b);
           }
}
int main()
{
    long long a,b,ans;
    while(cin>>a>>b)
    {
        ans=0;
        ans=gcd(a,b);
        if(ans==1)
        {
            cout<<"yes"<<endl;
        }
        else cout<<"no"<<endl;
    }

    return 0;
}

7 最大公约数和最小公倍数问题
Problem:2067
Time Limit:1000ms
Memory Limit:65535K
Description
输入2个正整数x和y (2<=x<=100000,2<=y<=1000000),求,满足下列条件的p,q的个数?
条件:
p,q是正整数,且p和q的最大公约数是x,最小公倍数是y;
试求:满足条件的所有可能的2个正整数的个数.
Input
输入x和y
Output
输出满足条件的p,q的个数
Sample Input
3 60
Sample Output
4
Hint
对于样例:有4种,
3,60
15,12
12,15
60,3

Source

for来列出p和q
#include <iostream>
using namespace std; 
int gcd(int a,int b){
	return b==0?a:gcd(b,a%b);
}
int main() 
{
    int x,y,p,q,ans=0;
    cin>>x>>y;
   for(p=x;p<=y;p++) //一层循环就够了,根据他们的关系直接求出另一个数
   {
   	  if(x*y%p) continue; //不是整除直接跳过
   	  q=x*y/p;
   	 // int yue=q<p?gcd(q,p):gcd(p,q);似乎两个数的顺序没要求今天才知道
   	  int yue=gcd(q,p); //这样也能过
   	  if( yue==x && (q*p)/yue==y) //公约数=x,公倍数=y
   	  ans++;  //是一种方案
   }
   cout<<ans;
   
	return 0;
}

8 又见GCD
Problem:992
Time Limit:1000ms
Memory Limit:65536K
Description
有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b。若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c。
Input
每行输入两个正整数a,b。
Output
输出对应的c,每组测试数据占一行
Sample Input
6 2
12 4
Sample Output
4
8
Hint
Source

#include <bits/stdc++.h>

using namespace std;
int gcd(int a,int b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}
int main()
{
    int a,b,c;
    while(cin>>a>>b)
    {
        c=2*b;//从2*b到3*b。。。开始
        while(c)
        {if(gcd(a,c)==b)
        {
           break;
        }
        c=c+b;
        }
        cout<<c<<endl;
    }
    return 0;
}

9 人见人爱gcd (需要找出关系)
Problem:1221
Time Limit:1000ms
Memory Limit:65535K
Description
x+y=a,lcm(x,y)=b;已知a和b求解x2+y2
Input
多组数据输入。
第一行一个t表示a,b数对的数量。
接下来t行2个数表示a,b
T<=100000
a,b<10^9;
Output
每组样例输出t行每行一个数表示x2+y2;
Sample Input
2
6 4
6 3
Sample Output
20
18
Hint
Source

#include <iostream>

#include <stdio.h>

#define ll long long

using namespace std;

ll gcd(ll a,ll b)

{

    return b?gcd(b,a%b):a;

}

int main()

{

    ll t;

    ll a,b;

    while(~scanf("%lld",&t)&&t)

    {

        while(t--)

        {  scanf("%lld%lld",&a,&b);

            ll ans=gcd(a,b);

            printf("%lld\n",a*a-2*ans*b);

        }

    }

    return 0;

}

10 gcd
Problem:1653
Time Limit:1000ms
Memory Limit:65535K
Description
羽裳记得有个高中同学叫开花,她会把1/2+1/2算成1/2,1/2+1/3算成2/5,1/3+1/4算成2/7。后来羽裳才摸清开花的脑回路,原来开花算分数相加的时候会把分母和分母相加,分子和分子相加,得到的新分数再进行约分。现在羽裳想用程序模拟一下开花的脑回路,羽裳写了一个程序,你输入a,b,c,d后,这个程序会按照开花的脑回路算出结果。
说到结果,我就想起了花果山,我就想到了今年下半年……中美合拍……开花……
Input
输入四个整数,a,b,c,d;
1<a<100
b,c,d的范围同a的范围
Output
输出按开花的脑回路计算a/b+c/d得出的结果
Sample Input
样例1:
1 2 3 4
样例2:
1 1 1 1
Sample Output
样例1:
2/3
样例2:
1
Hint
Source

#include <iostream>

using namespace std;
int gcd(int a,int b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}
int main()
{
    int a,b,c,d,a1,a2,a3,ans1,ans2;
    while(cin>>a>>b>>c>>d)
    {
        a1=a+c;a2=b+d;
        a3=gcd(a1,a2);
        ans1=a1/a3;
        ans2=a2/a3;
        if(ans1%ans2==0)
            cout<<ans1/ans2<<endl;
        else cout<<ans1<<"/"<<ans2<<endl;
    }
    return 0;
}

11 a/b + c/d
Problem:204
Time Limit:1000ms
Memory Limit:65536K
Description
给你2个分数,求他们的和,并要求和为最简形式。
Input
输入首先包含一个正整数T(T<=1000),表示有T组测试数据,然后是T行数据,每行包含四个正整数a,b,c,d(0< a,b,c,d< 1000),表示两个分数a/b 和 c/d。
Output
对于每组测试数据,输出两个整数e和f,表示a/b + c/d的最简化结果是e/f,每组输出占一行。
Sample Input
2
1 2 1 3
4 3 2 3
Sample Output
5 6
2 1
Hint
Source

#include <bits/stdc++.h>

using namespace std;
int gcd(int a,int b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}
int main()
{
    int t,a,b,c,d,x,y,z,ans1,ans2;
    while(cin>>t)
    {
        while(t--)
        {
            cin>>a>>b>>c>>d;
            x=b*d;y=a*d+c*b;
            z=gcd(x,y);
             ans1 = x/gcd(x,y);
             ans2 = y/gcd(x,y);
  cout << ans2 << ' ' << ans1 << endl;
        }
    }
    return 0;
}

12 最小公倍数
Problem:763
Time Limit:1000ms
Memory Limit:65536K
Description
给定两个正整数a和b,你的任务就是求它们的最小公倍数,a和b的范围均在long long内,题目保证运算后得到的最小公倍数在long long范围内。
Input
输入数据有多组,每组1行,分别是a和b。
Output
输出一个数,即a和b的最小公倍数。
Sample Input
12 4
30 12
Sample Output
12
60
Hint
Source

#include <bits/stdc++.h>

using namespace std;
int gcd(long long  a,long long  b)
{
   if(b==0) return a;
   else
   {
        return gcd(b,a%b);
   }
}
int main()
{
    long long a,b,c,ans;
    while(cin>>a>>b)
    {
        c=gcd(a,b);
        ans=a*b/c;
        cout<<ans<<endl;
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值