标签:
用二分法求下面方程在(-10, 10)之间的一个根。
2x3- 4x2+ 3x- 6 = 0
0
2.00000000
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 double x; 5 double judge(double xx) 6 { 7 return (2*xx*xx*xx-4*xx*xx+3*xx-6); 8 } 9 int main() 10 { 11 double left,right,mid,n,sum1,sum2,sum3; 12 scanf("%lf",&x); 13 left=-10; right=10; 14 while (right>left) 15 { 16 mid=(left+right)/2; 17 sum1=judge(left); 18 sum2=judge(right); 19 sum3=judge(mid); 20 if (sum3==0 || right-left<=x) 21 { 22 printf("%.8lf",mid); 23 return 0; 24 } 25 else 26 if (sum1*sum3<0) 27 right=mid; 28 else 29 if (sum2*sum3<0) 30 left=mid; 31 } 32 printf("No Solotion"); 33 return 0; 34 }
标签:
原文地址:http://www.cnblogs.com/DMonster/p/5004888.html