NYOJ 503(二分解方程)
解方程
时间限制:1000 ms | 内存限制:65535 KB
难度:3
- 描述
-
Now,given the equation 8*x^4 - 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky.- 输入
- The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);
- 输出
- For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.
- 样例输入
-
2 100 -4
- 样例输出
-
2.0422 No solution!
exp(-10)=e^-10;
1e-10=10^-10;因此用exp(-10)会wa#include<stdio.h> #include<math.h> double fun(double x) { double y=8*pow(x,4)-7*pow(x,3)+2*pow(x,2)+3*x+6; return y; } int main() { int a,b,n,T; double x1,x2,mid,m; scanf("%d",&T); while(T--) { scanf("%lf",&m); x1=0; x2=100; if(m<fun(0)||m>fun(100))/*值域符号相同则无解*/ { printf("No solution!\n"); continue; } while((x2-x1)>1e-10)/*用exp(-10)就wa*/ { mid=(x2+x1)/2.0; if(fun(mid)<m) x1=mid; else x2=mid; } printf("%.4lf\n",mid); } }
作者:火星十一郎
本文版权归作者火星十一郎所有,欢迎转载和商用,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.