UVA 10341 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-x + q*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: 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 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

首先对函数求导,发现是一个递减函数,这样就可以用二分求答案了,不过这个题精度要求好高啊,必须到10^-8次啊,一开始弄了一个10^-6,结果WA了,看来很久没有感觉有错误,就改了一下8就通过了,晕啊!


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

const double esp=1e-8;

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

double get_value(double x)
{
return p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u;
}

double find(double x,double y)
{
while (y-x>esp)
{
double m=x+(y-x)/2;
if (get_value(m)-esp>0) x=m;
else y=m;
}
return x;
}

int main()
{
while (scanf("%lf%lf%lf%lf%lf%lf",&p,&q,&r,&s,&t,&u)!=EOF)
{
if (get_value(0.0)<0 || get_value(1.0)>0)
printf("No solution\n");
else
printf("%.4lf\n",find(0.0,1.0));
}
return 0;
}

UVA 10341 Solve It,码迷,mamicode.com

时间: 2024-10-09 05:51:24

UVA 10341 Solve It的相关文章

UVA - 10341 - Solve It (二分求解)

思路:给你一个公式,求零点,从题目条件可以看出,此函数式是递减的,所以只要从两头往中间二分答案即可,注意精度问题,因为要精确到小数点后4位,<1e-6居然还WA,<1e-9才过,所以说尽量使精度高点 这里e的n次方可以用exp(n)表示,也可以用pow(M_E, n)表示 以下是math.h中定义的一些常量: /* Definitions of useful mathematical constants * M_E - e * M_LOG2E - log2(e) * M_LOG10E - lo

UVa 10341 (二分求根) Solve It

很水的一道题,因为你发现这个函数是单调递减的,所以二分法求出函数的根即可. 1 #include <cstdio> 2 #include <cmath> 3 //using namespace std; 4 5 const double e = 1e-14; 6 double p, q, r, s, t, u; 7 8 inline double f(double x) 9 { return p*exp(-x) + q*sin(x) + r*cos(x) + s*tan(x) +

UVA 10341

                                                                                    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 +

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

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

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-x+ q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and ter

UVa 11734 - Big Number of Teams will Solve This

题目:一个ACM的判题的小程序,两组字符全相同,为正确,比标准多输出空格,为格式错误,其他为错误. 分析:字符串.从前向后扫描,如果两字符不同,若A串当前字符不是空格,则错误: 若是空格,则一定不会是正确,滤过空格,看剩余部分,如果剩下字符相同则格式错误: 否则,一定错误: 说明:注意结束位置的空格.想起几年前开发自己OJ的日子了. #include <iostream> #include <cstdlib> #include <string> #include <

UVa 10642 - Can You Solve It?

题目:二维平面上的整数点,用路径链接起来(0,0)->(1,0)->(0,1)->(2,0)->.. 给你两点坐标,求两点间步长(在路径上的距离). 分析:简单题. 我们发现点是按照x+y的递增序,且y的递增序(x+y相同时)排列的: 所以每个点对应的路径上的位置为:(x+y)*(x+y+1)/ 2 + y. 说明:注意使用long long防止数据溢出. #include <algorithm> #include <iostream> #include &

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f