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 file.
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;
}
代码

 

posted @ 2015-12-23 23:59  meekyan  阅读(227)  评论(0编辑  收藏  举报