UVA 10341

                                                                                    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,t and u (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 should be a line containing the value ofx, 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


函数单调递减 二分查找,高中知识,区间头尾对应的值相乘为负数,开始我分左右两边,用二分查找找他们相等,虽然给的样例过了,
但一直wa,也许是精度缺失了把,最后选择这样的
 1 #include<iostream>
 2 #include<cmath>
 3 #include<cstdio>
 4 using namespace std;
 5 int p,q,r,s,t,u;
 6 double fun(double x)
 7 {
 8     double y;
 9     y = p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u;
10     return y;
11 }
12 int main()
13 {
14     double m,n;
15     while(cin>>p>>q>>r>>s>>t>>u)
16     {
17         double max=1.0;
18         double min=0.0;
19         if(fun(max)*fun(min)>0)
20             cout<<"No solution"<<endl;
21         else
22         {
23             double mid;
24             while(min+0.00000001<=max)
25             {
26                 mid=(max+min)/2.0;
27                 if(fun(mid)<=0)
28                     max=mid;
29                 else
30                     min=mid;
31             }
32             printf("%.4lf\n",mid);
33         }
34     }
35     return 0;
36 }

 

posted on 2016-07-20 20:44  见字如面  阅读(237)  评论(0编辑  收藏  举报

导航