首页 > 代码库 > UVA 10341

UVA 10341

UVA 10341

 

主要是要知道这个函数是单调递减的,这样就好做了。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 double p,q,r,s,t,u;
 5 double fun(double x){
 6     return p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*(x)*(x)+u;
 7 }
 8 int main(){
 9     while(scanf("%lf %lf %lf %lf %lf %lf",&p,&q,&r,&s,&t,&u)!=EOF){
10         if(fun(0) < 0 || fun(1) > 0){
11             printf("No solution\n");
12             continue;
13         }
14         int flag = 0;
15         double i = 0, j = 1, mid;
16         while(1){
17             mid = (i+j)/2.0;
18             if(fabs(fun(mid)) < 1e-9){
19                 printf("%.4lf\n",mid);
20                 flag = 1;
21                 break;
22             }
23             if(fun(mid) > 0) i = mid;
24             else if(fun(mid) < 0) j = mid;
25         }
26         if(!flag)printf("No solution\n");
27     }
28     return 0;
29 }

 

UVA 10341