已知两个正整数的和为667,它们的最大公约数与最小公倍数之比为1:120求这两个数。
注:要求不能编写或是调用求两个数的最大公约数或最小公倍数的函数来求解,而且程序的运算速度要尽量快。
已知两个正整数的和为667,它们的最大公约数与最小公倍数之比为1:120求这两个数。
注:要求不能编写或是调用求两个数的最大公约数或最小公倍数的函数来求解,而且程序的运算速度要尽量快。
 2006-09-27 19:23
	    2006-09-27 19:23
  
#include<stdio.h>
#include<math.h>
#define SUM 667
int main()
{
    int t,n=1,sq,x1,x2;
    while((t=SUM*SUM-4*120*n*n)>=0)
    {
        sq=(int) sqrt(t);
        if(sq*sq==t && (SUM+sq)%2==0)
        {
            x1=(SUM+sq)/2;
            x2=(SUM-sq)/2;
            if(x1>0 && x2>0 && x1%n==0 && x2%n==0)
                printf("the two numbers are %d,%d\n",x1,x2);
        }
        n++;
    }
    return 0;
}

 2006-09-27 21:34
	    2006-09-27 21:34
  方法2:
#include<stdio.h>
#include<math.h>
int main()
{
    int i;
    for(i=1;i<=sqrt(120);i++)
    {
        if(120%i==0 && 667%(i+120/i)==0)
            printf("the two numbers are %d,%d\n",i*667/(i+120/i),120/i*667/(i+120/i));
    }
}

 2006-09-27 21:43
	    2006-09-27 21:43
  
#include<stdio.h>
#include<math.h>
#define SUM 667
int main()
{
    int t,n=1,sq,x1,x2;
    while((t=SUM*SUM-4*120*n*n)>=0)
    {
        sq=(int) sqrt(t);
        if(sq*sq==t && (SUM+sq)%2==0)
        {
            x1=(SUM+sq)/2;/*写的不好!如果SUM+sq是个奇数哪就不正确了,但是实际上SUM+sq是个偶数,何不改为   x1=SUM+sq; x2=SUM-sq; if(x1>0 && x2>0 && x1%(2*n)==0 && x2%(2*n)==0)
                printf("the two numbers are %d,%d\n",x1/2,x2/2);*/
            x2=(SUM-sq)/2;
            if(x1>0 && x2>0 && x1%n==0 && x2%n==0)
                printf("the two numbers are %d,%d\n",x1,x2);
        }
        n++;
    }
    return 0;
}
总之这个程序是用方程的观点计算出x1与x2的,写的不错!呵呵!

 2006-10-04 17:21
	    2006-10-04 17:21
  [QUOTE]
#include<stdio.h>
#include<math.h>
#define SUM 667
int main()
{
    int t,n=1,sq,x1,x2;
    while((t=SUM*SUM-4*120*n*n)>=0)
    {
        sq=(int) sqrt(t);
        if(sq*sq==t && (SUM+sq)%2==0) //这个不能保证SUM+sq是偶数吗?
        {
            x1=(SUM+sq)/2;/*写的不好!如果SUM+sq是个奇数哪就不正确了,但是实际上SUM+sq是个偶数,何不改为   x1=SUM+sq; x2=SUM-sq; if(x1>0 && x2>0 && x1%(2*n)==0 && x2%(2*n)==0)
                printf("the two numbers are %d,%d\n",x1/2,x2/2);*/
            x2=(SUM-sq)/2;
            if(x1>0 && x2>0 && x1%n==0 && x2%n==0)
                printf("the two numbers are %d,%d\n",x1,x2);
        }
        n++;
    }
    return 0;
}
[/QUOTE]

 2006-10-04 21:20
	    2006-10-04 21:20
  
 2006-10-20 19:29
	    2006-10-20 19:29
   2006-10-20 20:16
	    2006-10-20 20:16
   2006-10-21 20:34
	    2006-10-21 20:34
  
#include<stdio.h>
#include<math.h>
#define SUM 667
int main()
{
    int t,n=1,sq,x1,x2;
    while((t=SUM*SUM-4*120*n*n)>=0)
    {
        sq=(int) sqrt(t);
        if(sq*sq==t && (SUM+sq)%2==0)
        {
            x1=(SUM+sq)/2;
            x2=(SUM-sq)/2;
            if(x1>0 && x2>0 && x1%n==0 && x2%n==0)
                printf("the two numbers are %d,%d\n",x1,x2);
        }
        n++;
    }
    return 0;
}
你知道你写的这个程序为什么没有输出结果吗?我刚才运行了一下发现你的程序有毛病!

 2006-10-23 21:19
	    2006-10-23 21:19
   2006-10-24 21:58
	    2006-10-24 21:58