首页 > 其他 > 详细

2.最大公约数和最小公倍数问题

时间:2016-03-18 07:07:50      阅读:151      评论:0      收藏:0      [点我收藏+]

题目描述 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\n");

              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;

}

2.最大公约数和最小公倍数问题

原文:http://www.cnblogs.com/csgc0131123/p/5290281.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!