UVA 10341 Solve It 二分

来源:互联网 发布:乐天市场相当于淘宝吗 编辑:程序博客网 时间:2024/05/13 18:56
#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <iostream>#include <algorithm>using namespace std;#define f(x) (p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*(x)*(x)+u)const double eps=1e-14;int main(){    int p,q,r,s,t,u;    while(scanf("%d%d%d%d%d%d",&p,&q,&r,&s,&t,&u)!=EOF)    {        if(f(1)>eps||f(0)<-eps)printf("No solution\n");        else        {            double x=0,y=1.0,mid;            while(y-x>1e-7)//1e-6以上错误。。            {                mid=(x+y)/2;                if(f(mid)<0)y=mid;                else x=mid;            }            printf("%.4lf\n",mid);        }    }    return 0;}/*    方程左边构成的函数式减函数,所以当f(0)>=0&&f(1)<=0时有唯一解,否则无解*/

原创粉丝点击