UVA Solve It(二分查找)

Problem F

Solve It

Input: standard input

Output: standard output

Time Limit: 1 second

Memory Limit: 32 MB

Solve the equation:

p*e-xq*sin(x) + r*cos(x) + s*tan(x) + t*x2 + 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: pqrst 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 upto 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


题意:找到一个数X满足 p*e-xq*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0

代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

using namespace std;

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

double findx(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)!=EOF)
    {
        if(findx(0)<0 || findx(1)>0)
        {
            printf("No solution\n");
        }
        else
        {
            double x1 = 0;
            double x2 = 1;
            while(fabs(x1-x2)>=1e-10)
            {
                double x = (x1+x2)/2;
                if(findx(x)>0)
                {
                    x1 = x;
                }
                else
                {
                    x2 = x;
                }
            }
            printf("%.4lf\n",x1);
        }
    }
    return 0;
}


Mustaq Ahmed

时间: 2024-08-06 11:49:11

UVA Solve It(二分查找)的相关文章

uva10341 - solve it (二分查找)

题目:uva10341-solve it 题目大意:求解给定的方程式解题思路:因为这个方程式在给定的x的范围内是单调递减的,所以可以用二分查找来尝试x的值.这里的 x是要求保留4小数,所以当区间缩小到一定的范围,这时候就是x的解.无解的情况只可能出现在x范围的两端. 代码: #include <stdio.h> #include <stdlib.h> #include <math.h> double p, q, r, s, t, u; const double eps

uva:10487 - Closest Sums(二分查找)

题目:10487 - Closest Sums 题目大意:给出一组数据,再给出m个查询的数字.要求找到这组数据里的两个数据相加的和最靠近这个查询的数据,输出那两个数据的和. 解题思路:二分查找,这样找到的话,就输出查询的数值,但是要注意找不到的情况:这里最靠近的值不一定是在找不到的时刻的前一次数据,所以要维护最靠近的要查询数的数值. 代码: #include <stdio.h> #include <algorithm> #include <stdlib.h> using

UVa 1152 和为0的4个值(二分查找)

https://vjudge.net/problem/UVA-1152 题意:给定4个n元素集合A,B,C,D,要求分别从中选取一个元素a,b,c,d,使得a+b+c+d=0.问有多少种取法. 思路:直接暴力枚举的话是会超时的.可以选把a+b的值枚举出来存储,c和d的值也一样并排序,这样就可以在c和d中进行二分查找了. 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int ma

UVA 10341 (二分查找+精度)

题意: 给你一个关于x的方程,给出变量的值,求出x: Problem F Solve It Input:standard input Output:standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x+ q*sin(x) + r*cos(x) +s*tan(x) +t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of mult

HDOJ-2199-Can you solve this equation?(二分查找)

Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11044    Accepted Submission(s): 5083 Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 ==

uva 10487 Closest Sums (遍历&amp;二分查找&amp;&amp;双向查找)

题目大意:先给定n个数字,现在要求算出这n个数字的两两之和保存到sum数组,然后在给定m个数,要求找到和每一个数最接近的sum[i]: 挨个计算每个属于其他数之间的sum,然后排序: 查找时有两种方法:二分查找&&双向查找:当然二分查找的效率比后者高了很多,但是都能AC. 提供一条新思路,并不一定非要用二分. 双向查找: #include<stdio.h> #include<algorithm> #include<stdlib.h> using name

UVa 10539 (筛素数、二分查找) Almost Prime Numbers

题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序. 对于L和U,二分查找出小于U和L的最大数的下标,作差即可得到答案. 1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 5 typedef long long LL; 6 7 const int maxn = 10

uva 10763 Foreign Exchange(排序+二分查找)

这道题是我第一次算出来应该用什么复杂度写的题,或许是这一章刚介绍过,500000的数据必须用nlogn,所以我就 想到了二分,它的复杂度是logn,再对n个数据进行遍历,正好是nlogn,前两次TLE了,然后我就对我的做法没信心 了...看到一篇博客上说就应该这种方法,然后我就坚定的改自己的算法去了,哈哈,专注度没有达到五颗星,最多 三颗... 思路: 我用的是结构体保存的,先对每一对序列排序,然后对第二个元素在第一个元素中二分搜索,用到了 lower_bound,upper_bound,注释里

CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] Description Give you n packs, each of it has a value v and a weight w. Now you should find some packs, and the total of these value is max, total of

【二分查找最优解】FZU 2056 最大正方形

题意:现在有一个n*m的矩阵A,在A中找一个H*H的正方形,使得其面积最大且该正方形元素的和不大于 limit. 分析:开始以为是DP或者二维RMQ,其实用二分就可以做出来: 在输入时构造元素和矩阵dp[][](即dp[i][j]为从(1,1)到(i,j)的矩形范围元素和);再在(0,min(m,n))范围内二分查找满足条件的最优解H:计算正方形内元素和的方法要掌握; 注意二分时要避免出现L==M而死循环的情况. 代码如下: 1 #include<iostream> 2 #include<