• 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
    ");continue;
            }
            else if(fabs(check(0.0)) <= eps){
                printf("0.0000
    ");continue;
            }
            else if(fabs(check(1.0)) <= eps) {
                printf("1.0000
    ");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
    ",x);
                    break;
                }
                else if(temp > 0) {
                    b = x;
                }
                else  a = x;
            }
        }
        return 0;
    }
    代码
  • 相关阅读:
    1774:大逃杀
    Angular实现简单数据计算与删除
    IDEA 如何搭建maven 安装、下载、配置(图文)
    win10 Java JDK环境变量配置
    Nginx学习使用
    ASP.NET Core中返回 json 数据首字母大小写问题
    mysql使用遇到的问题
    线程同步以及AutoResetEvent
    Device Class
    Xamarin.Forms之布局压缩
  • 原文地址:https://www.cnblogs.com/zxhl/p/5071728.html
Copyright © 2020-2023  润新知