uva 10341 - Solve It

点击打开链接uva 10341

题目意思:     给定一个函数的表达式,然后给定系数值,要我们求出未知数x

解题思路:    1:由函数的性质可知,如果有解,那么将区间两个点带入求出的数值的乘积为负数。首先先判断这个函数是否有解,如果无解直接返回输出,有解进行二分查找
                      2:二分查找时候由于0=<x<=1,那么初始化left = 0 , right = 1,我习惯用迭代二分,所以这里用while循环代替递归,但是由于double类型的比较会有很大的误差,所以用left-right < 1e-9来判断相等而不是直接left-right == 0,然后我么用mid来带入函数,如果得到的值是大于0,那么这里是要向右递归的,因为这个函数是递减的,由f(0) > f(1) 可以知道,所以这里很平常的有些不同。
                      3:注意迭代二分的使用,注意double类型的比较

代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
using namespace std

double p , q , r , s , t , u;

void solve(){
    double left , right , mid  , tmp;
    double s1 , s2;
    s1 = p*(exp(0))+q*sin(0)+r*cos(0)+s*tan(0)+t*0+u;
    s2 = p*(exp(-1))+q*sin(1)+r*cos(1)+s*tan(1)+t*1+u;

    if(s1*s2 > 0) printf("No solution\n");
    else{
        left = 0.0 ; right = 1.00;
        while(right-left > 1e-9){//注意判断条件
            mid = (left +right)/2;//中间值
            tmp = p*(exp(-mid))+q*sin(mid)+r*cos(mid)+s*tan(mid)+t*mid*mid+u;//求出函数值
            if(tmp < 0) right = mid;//小于0则要向左边二分
            else  left = mid;//否则右边二分
        }
        printf("%.4lf\n" , mid);//输出的mid就是x值
    }
}

int main(){
    //freopen("input.txt" , "r" , stdin);
    while(scanf("%lf%lf%lf%lf%lf%lf" , &p,&q,&r,&s,&t,&u) != EOF)
        solve();
    return 0;
}
时间: 2024-08-03 15:44:04

uva 10341 - Solve It的相关文章

UVa 10341: Solve It

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1282 原题: Solve the equation:        p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0        where 0 <= x <= 1. Input

UVa 10057:A mid-summer night&#039;s dream.

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=998 原题: This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here because thi

UVa 108:Maximum Sum

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=44 原题: Background A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dim

UVa 10827:Maximum sum on a torus

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1768 原题: A grid that wraps both horizontally and vertically is called a torus. Given a torus where each cell contains an inte

UVa 270:Lining Up

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=206 原题: ``How am I ever going to solve this problem?" said the pilot. Indeed, the pilot was not facing an easy task. She h

UVa 10718:Bit Mask

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1659 [原题] In bit-wise expression, mask is a common term. You can get a certain bit-pattern using mask. For example, if you wan

UVa 10670:Work Reduction

[链接] UVA:  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1611 poj:     http://poj.org/problem?id=1907 [原题] Paperwork is beginning to pile up on your desk, and tensions at the wo

UVa 10716: Evil Straw Warts Live

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1657 [原题] A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a

UVa 11100:The Trip, 2007

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=2041 [原题] A number of students are members of a club that travels annually to exotic locations. Their destinations in the past