Uva - 10341 - Solve It(二分)

题意:解方程

p*e-x q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0
        where 0 <= x <= 1.(0 <= p,r <= 20 and -20 <= q,s,t <= 0

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=493&problem=1282

——>>仔细观察可发现每一项在定义的域上都是减函数,所以,直接二分吧……

第一条数值题,学了3样东西:

1、二分迭代100次;

2、浮点数比较用个误差eps = 1e-14,判正数用eps,判负数用-eps;

3、函数式可以用#define来定义喔……

#include <cstdio>
#include <cmath>

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, i;
    while(~scanf("%d%d%d%d%d%d", &p, &q, &r, &s, &t, &u))
    {
        double f0 = F(0), f1 = F(1);
        if(f0 < -eps || f1 > eps) printf("No solution\n");
        else
        {
            double m, L = 0, R = 1;
            for(i = 0; i < 100; i++)
            {
                m = L + (R-L) / 2;
                if(F(m) >= 0) L = m;
                else R = m;
            }
            printf("%.4lf\n", L);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值