我编写了一个求正整数算数平方根的程序,不知道错哪了!请大虾们指教。
来源:百度知道 编辑:UC知道 时间:2024/05/17 04:15:58
程序如下:
main()
{
int n ,w,i;
unsigned double ss;
unsigned double x, y;
printf("enter the number!\n");
scanf("%lf",&y);
x=y;
ss=1.0;
for(w=0;x>=1;w++)
{x=x/10;}
w=w-1;
for(i=1;i<=w;i++)
{ss=ss*10;}
x=0;
while(ss>1.0/10e4)
{
for(;x*x<y;x=x+ss)
if (x*x>y)
{
x=x-ss;
ss=ss/10;
}
if (x*x==y)
break;
}
printf("%lf",x);
}
main()
{
int n ,w,i;
unsigned double ss;
unsigned double x, y;
printf("enter the number!\n");
scanf("%lf",&y);
x=y;
ss=1.0;
for(w=0;x>=1;w++)
{x=x/10;}
w=w-1;
for(i=1;i<=w;i++)
{ss=ss*10;}
x=0;
while(ss>1.0/10e4)
{
for(;x*x<y;x=x+ss)
if (x*x>y)
{
x=x-ss;
ss=ss/10;
}
if (x*x==y)
break;
}
printf("%lf",x);
}
思路不要,要好好的整理一下思路,
我已经为你改好了,不过没有对负数进行判断
#include <stdio.h>
void main()
{
int w,i;
double ss;
double x, y;
printf("enter the number!\n");
scanf("%lf",&y);
x=y;
ss=1.0;
for(w=0;x>=1;w++)
{x=x/10;}
w=w-1;
for(i=1;i<=w;i++)
{ss=ss*10;}
x=0;
while(ss>1.0/10e4)
{
for(;x*x<y;x=x+ss);
if (x*x>y)
{
x=x-ss;
ss=ss/10;
}
if (x*x==y)
break;
}
printf("%lf",x);
}