Solve It
Input:Standard input
Output:Standard output
Time Limit:1 second
Memory Limit: 32 MB
Solve the equation:
P* E-X+Q* Sin (X) +R* Cos (X) +S* Tan (X) +T*X2 +U= 0
Where 0 <=X<= 1.
Input
Input consists of multiple test cases and terminated by an EOF. Each test case consists of 6 integers in a single line:P,Q,R,S,TAndU(Where0 <=P,R<= 20 and-20 <=Q,S,T<= 0). There will be maximum 2100 lines in the input file.
Output
For each set of input, there shoshould be a line containing the valueX, Correct upto 4 decimal places, or the string "No solution", whichever is applicable.
Sample Input
0 0 0 0 -2 1
1 0 0 0 -1 2
1 -1 1 -1 -1 1
Sample Output
0.7071
No solution
0.7554
Question Analysis:
For the first time, we encountered a binary equation. The first time I did not come out, I had two consecutive questions after being instructed by my classmates. Very nice ^ O ^. The five coefficients p, q, r, s, and u are given. Ask you to find the solution of the equation.
Analysis of ideas:
We learned it when we were in middle school, and obtained the binary iteration. Therefore, we can start from this. That is, when f (x1) * f (x2) <= 0, the equation must have a solution. Okay. Now that we have solved the problem of determining whether the equation has a solution, how can we get the solution? Obviously, we can follow the binary iteration we just mentioned. Based on the knowledge in advanced mathematics, we can determine the monotonicity of an equation. Therefore, binary classification can be performed based on monotonicity.
Next we will give different judgment conditions based on the monotonicity of the function:
Monotonic increasing:
Mid = (x1 + x2)/2;
--------> F (mid) <0x1 = mid;
--------> F (mid)> 0x2 = mid;
For example, assume that the required solution is 2.
Monotonic decrease:
Mid = (x1 + x2)/2;
--------> F (mid)> 0x1 = mid;
-------> F (mid) <0x2 = mid;
For example, if the required solution is 4.
OK. That's all about the basic algorithms. The other is the detailed issue. Then, pay attention to the function representation of e ^ x. At the beginning, I did not know how to represent it before I knew e ^ x = exp (x ); read the code by yourself.
#include
#include #include
#include
#include
#include
using namespace std;const double EPS = 1e-14;double p,q,r,s,t,u;double Check(double x){ double y; y = p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u; return y;}int main(){ while(scanf("%lf%lf%lf%lf%lf%lf",&p,&q,&r,&s,&t,&u)!=EOF) { double x1 = 0.0,x2 = 1.0; if(Check(x1)*Check(x2)<=EPS){ double mid; while(x2-x1>EPS) { mid = (x1+x2)/2.0; if(Check(mid) < 0) x2 = mid; else x1 = mid; } printf("%.4lf\n",mid); }else { printf("No solution\n"); } } return 0;