codeVS之旅:1012 最大公约数和最小公倍数问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xckkcxxck/article/details/83069656

1012 最大公约数和最小公倍数问题      http://codevs.cn/problem/1012/

2001年NOIP全国联赛普及组

 时间限制: 1 s

 空间限制: 128000 KB

 题目等级 : 白银 Silver

题解

 查看运行结果

题目描述 Description

输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数

条件:  1.P,Q是正整数

2.要求P,Q以x0为最大公约数,以y0为最小公倍数.

扫描二维码关注公众号,回复: 3638468 查看本文章

试求:满足条件的所有可能的两个正整数的个数.

输入描述 Input Description

二个正整数x0,y0

输出描述 Output Description

满足条件的所有可能的两个正整数的个数

样例输入 Sample Input

3 60

样例输出 Sample Output

4

数据范围及提示 Data Size & Hint

解答:首先我想到了最大公约数与最小公倍数的关系,以及这一题应该是从最大公约数到最小公倍数进行遍历,然后想到了利用递归求最大公约数的方法,结合以上几点,此问题就可以求解了,在这要注意遍历的范围等等容易出错的地方。

#include<bits/stdc++.h>
using namespace std;

int yue(int a, int b)
{
    return b==0?a:yue(b, a%b);
}

int main()
{
    int x0, y0;
    cin>>x0>>y0;
    int count=0;
    int j=0;
    for(int i=x0; i<=y0; i++)
    {
        j = x0*y0/i;
        if(i*j != x0*y0) continue;
        if(i<=j)
        {
            if(yue(j, i)==x0) count++;
        }
        else
        {
            if(yue(i, j)==x0) count++;
        }
    }
    cout<<count<<endl;
    return 0;

}

猜你喜欢

转载自blog.csdn.net/xckkcxxck/article/details/83069656