50 lines
997 B
C
50 lines
997 B
C
|
#include <stdio.h>
|
|||
|
#include <math.h>
|
|||
|
|
|||
|
#define EPSILON 1e-6
|
|||
|
#define MAX_ITERATIONS 100
|
|||
|
|
|||
|
double f(double x, double a) {
|
|||
|
return x * x - a;
|
|||
|
}
|
|||
|
|
|||
|
double bisection_method(double a, double left, double right) {
|
|||
|
double mid;
|
|||
|
int iterations = 0;
|
|||
|
|
|||
|
while (right - left > EPSILON && iterations < MAX_ITERATIONS) {
|
|||
|
mid = (left + right) / 2;
|
|||
|
|
|||
|
if (f(mid, a) == 0.0) {
|
|||
|
return mid;
|
|||
|
} else if (f(mid, a) * f(left, a) < 0) {
|
|||
|
right = mid;
|
|||
|
} else {
|
|||
|
left = mid;
|
|||
|
}
|
|||
|
|
|||
|
iterations++;
|
|||
|
}
|
|||
|
|
|||
|
return (left + right) / 2;
|
|||
|
}
|
|||
|
|
|||
|
int main() {
|
|||
|
double a, result;
|
|||
|
|
|||
|
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD>@<40>ӥ<EFBFBD><D3A5><EFBFBD> a <20>ӨD<D3A8><44> x^2 = a: ");
|
|||
|
scanf("%lf", &a);
|
|||
|
|
|||
|
if (a < 0) {
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD>~<7E>Ga <20><><EFBFBD><EFBFBD><EFBFBD>O<EFBFBD>D<EFBFBD>t<EFBFBD><74>\n");
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
result = bisection_method(a, 0, a > 1 ? a : 1);
|
|||
|
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD>{ x^2 = %.6f <20><><EFBFBD>Ѭ<EFBFBD><D1AC><EFBFBD> x = %.6f\n", a, result);
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD>ҡG%.6f^2 = %.6f\n", result, result * result);
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|