#include#include #define EPSILON 1e-7 double bisection(int p, int q, double (*func)(int, int, double)); double f(int p, int q, double x); int main() { int p; int q; scanf("%d%d", &p, &q); printf("%.4lfn", bisection(p, q, f)); return 0; } double bisection(int p, int q, double (*func)(int, int, double)) { //a,b 是开始选定解的区间,区间越小,计算越少 double a = -200.0; double b = 200.0; double m = (a + b)/ 2; while(fabs(func(p,q,m)) > EPSILON){ if((func(p,q,m) * func(p,q,a) < 0)){ b = m; m = (a + b)/2; }else{ a = m; m = (a + b)/2; } } return m; } double f(int p, int q, double x) { return p * x + q; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)