UVA10341二分解方程

这里没答案的情况怎么判断,我想复杂了,因为是单调函数,x的最大值和最小值带入方程同号肯定是没答案的,

否则肯定就是有答案的,还有就是这里二分的时候eps的大小,我设置的eps,1e-4,1e-5,1e-6都不过,1e-7才能过,

不知道为什么。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=1e3;
const double eps=1e-8;
const double e=2.71828;
double p,q,r,s,t,u;
double calc(double m)
{
    return p*exp(-m)+q*sin(m)+r*cos(m)+s*tan(m)+t*m*m+u;
}
int main()
{
    while(scanf("%lf%lf%lf%lf%lf%lf",&p,&q,&r,&s,&t,&u)!=EOF)
    {
        double x=0,y=1;
        if(calc(x)*calc(y)>0)
        {
            cout<<"No solution"<<endl;
            continue;
        }
        while(y-x>eps)
        {
            double m=x+(y-x)/2;
            if(calc(m)>0)
                x=m;
            else
                y=m;
        }
            printf("%.4f\n",x);

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值