Problem Description
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.
Input
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);
Output
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.
Sample Input
2
100
-4
Sample Output
1.6152
No solution!
题解:二分搜索,精度见代码。一直WA,找不到错,最后发现是少了一个感叹号!坑啊。
判断无解利用分析这个函数的导函数在[0,100]上恒大于0,所以这个函数在[0,100]上单调递增的性质来判断。
#include <cstdio> #include <iostream> #include <string> #include <sstream> #include <cstring> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <map> #define ms(a) memset(a,0,sizeof(a)) #define msp memset(mp,0,sizeof(mp)) #define msv memset(vis,0,sizeof(vis)) using namespace std; #define LOCAL int y; double fun(double x) { return 8*pow(x,4.0)+7*pow(x,3.0)+2*pow(x,2.0)+3*x+6; } void solve() { if(fun(0)>y||fun(100)<y) { printf("No solution!\n"); return; } double a=0,b=100,ans,m; while(b-a>1e-6) { m=(a+b)/2; ans=fun(m); if(ans>y)b=m-1e-7; else a=m+1e-7; } m=(a+b)/2.0; printf("%.4lf\n",m); return; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL //Start int N; cin>>N; while(N--) { cin>>y; solve(); } return 0; }
时间: 2024-10-13 06:13:17