UVA 10341 二分搜索
Solve the equation:
p ? e
?x + q ? sin(x) + r ? cos(x) + s ? tan(x) + t ? x
2 + 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 (where 0 ≤ p, r ≤ 20 and ?20 ≤ q, s, t ≤ 0). There will be maximum
2100 lines in the input ?le.
Output
For each set of input, there should be a line containing the value of x, correct up to 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
?
題意:求解這個等式
題解:我們分析下這個等式,就發現是個單調遞減的,二分[0,1]就好了
//meek///#include<bits/stdc++.h> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include<iostream> #include<bitset> using namespace std ; #define mem(a) memset(a,0,sizeof(a)) #define pb push_back #define fi first #define se second #define MP make_pair typedef long long ll;const int N = 100010; const int inf = 0x3f3f3f3f; const int MOD = 100003; const double eps = 0.000001;double q,p,r,s,t,u,a,b; double check(double x) {return p * exp(-x) + q * sin(x) + r * cos(x) + s * tan(x) + t * x * x + u; } int main() {while(~scanf("%lf%lf%lf%lf%lf%lf",&p,&q,&r,&s,&t,&u)) {double a = 0.0,b = 1.0;if(check(a) * check(b) > 0) {printf("No solution\n");continue;}else if(fabs(check(0.0)) <= eps){printf("0.0000\n");continue;}else if(fabs(check(1.0)) <= eps) {printf("1.0000\n");continue;}if(check(0)>0) {a = 1.0;b = 0.0;}else {b = 1.0;a = 0.0;}while(1) {double x = (a+b)/2;double temp = check(x);if(fabs(temp) <= eps) {printf("%.4f\n",x);break;}else if(temp > 0) {b = x;}else a = x;}}return 0; } 代碼?
轉載于:https://www.cnblogs.com/zxhl/p/5071728.html
總結
以上是生活随笔為你收集整理的UVA 10341 二分搜索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Operating System-进程/
- 下一篇: 【安装PHP】如何在openSUSE42