zoukankan      html  css  js  c++  java
  • 2.最大公约数和最小公倍数问题

    题目描述 Description

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

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

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

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

    输入描述 Input Description

    二个正整数x0,y0

    输出描述 Output Description

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

    样例输入 Sample Input

    3 60

    样例输出 Sample Output

    4

    源代码:

    #include

    using namespace std;

    #include

    #include

    int x,y,sum=0;

    int gys(int a,int b)

    {

           if(a

           swap(a,b);

           int sh=a-b;

           while(sh!=0)

           {

                  a=b;

                  b=sh;

                  if(a

                  swap(a,b);

                  sh=a-b;

           }

           return b;

    }

    int gbs(int a,int b)

    {

           return a*b/gys(a,b);

    }

    int main()

    {

           cin>>x>>y;

           if(x>y)

           swap(x,y);

           if(y%x!=0)

           {

                  printf("0 ");

                  return 0;

           }

           else{

                  int v=x*y;

                  int s=sqrt(v);

                  for(int i=x;i<=s;i+=x)

                  {

                         if(gys(i,v/i)==x&&gbs(i,v/i)==y)

                         sum++;

                  }

           }

           printf("%d",sum*2);//再*2输出就是了

           return 0;

    }

  • 相关阅读:
    代替gets()的新操作
    前缀和(一维与二维) 差分
    高精度(高精加,高精减,高精乘,高精除)
    提高cin cout的速度
    二分算法(以 数的范围 为例)
    归并排序(merge_sort)
    快速排序(quick_sort)
    由后缀表达式题目:stoi atoi 函数新发现
    Redis(二)
    每日算法练习(2020-1-11)
  • 原文地址:https://www.cnblogs.com/csgc0131123/p/5290281.html
Copyright © 2011-2022 走看看