Data_Structure/作業/unit1/binary-search-solves-equation/test_2.c
2025-01-20 21:30:53 +08:00

33 lines
525 B
C
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<stdio.h>
#include<stdlib.h>
#include<>
int main(){
double x,mid ,precision=1e-10;
int i=0, count=1000;
printf("¿é¤Jx­È");
scanf("%lf",&x);
double upper=x,lower=0;
mid=(upper+lower)/2;
while (fabs((mid*mid)-x)> precision && i < count){
mid=(upper+lower)/2;
printf("#%d\t[%f,%f]\t¸Ñ=%f\n",i+1,lower,upper,mid);
i++;
if(fabs(mid-x) <precision){
break;
}else if(mid*mid>x){
upper=mid;
// lower=mid;
}else{
// upper=mid;
lower=mid;
}
}
printf("%f",mid);
return 0;
}