-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10341.cpp
49 lines (45 loc) · 978 Bytes
/
P10341.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <stdio.h>
#include <cmath>
double f(int p, int q, int r, int s, int t, int u, double x) {
return p*exp(-x) + q*sin(x) + r*cos(x) + s*tan(x) + t*x*x + u;
}
#define EPS 1e-6
int main() {
int p, q, r, s, t, u;
while(std::cin >> p >> q >> r >> s >> t >> u) {
double fMin = f(p,q,r,s,t,u,0);
double fMax = f(p,q,r,s,t,u,1);
if(fMin == 0) {
printf("%.4f\n", 0.0);
continue;
}
if(fMax == 0) {
printf("%.4f\n", 1.0);
continue;
}
if((fMin > 0 && fMax > 0) || (fMin < 0 && fMax < 0)) {
printf("No solution\n");
continue;
}
double xMin = 0;
double xMax = 1;
while(true) {
double x = (xMin+xMax)/2;
double fx = f(p,q,r,s,t,u,x);
if(-EPS <= fx && fx <= EPS) {
printf("%.4f\n", x);
break;
}
if((fMin < 0 && fx < 0) || (fMin > 0 && fx > 0)) {
xMin = x;
fMin = fx;
}
else {
xMax = x;
fMax = fx;
}
}
}
return 0;
}