首页 > 代码库 > UVA - 10341-Solve It

UVA - 10341-Solve It

10341 - Solve It

Time limit: 3.000 seconds 

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 ?le.
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

 

题意就是解方程。。。

二分

代码:

#include<stdio.h>
#include<math.h>
#define eps 0.0000001
int p,q,r,s,t,u;
double fun(double x){
    double y;
    y=p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u;
    return y;
}
int main()
{
    while(scanf("%d%d%d%d%d%d",&p,&q,&r,&s,&t,&u)!=EOF)
    {
        double maxx=1.0,minn=0.0,mid;
        if(fun(maxx)*fun(minn)>0){
            printf("No solution\n");
            continue;
        }
          while(minn+eps<maxx){
                    mid=(maxx+minn)/2.0;
            if(fun(mid)<=0)
                maxx=mid;
            else
                minn=mid;
          }
          printf("%.4f\n",mid);
    }
    return 0;
}

 

UVA - 10341-Solve It