uva - 10341 - Solve It(二分查找,数学)

题意:解方程组,一共6项,对每项没来说有的是增函数,有的是减函数,但是由于系数的限制,所有加上系数后都是减函数,整个函数为减。

方法:二分查找。

注意:setprecision限制小数点,必须加上fixed,否则WA,亲测。

#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>

using namespace std;

#define cal(x) (p*exp(-x) + q*sin(x) + r*cos(x) + s*tan(x) + t*x*x + u)

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

int main()
{
#ifdef Local
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif
	while (cin >> p >> q >> r >> s >> t >> u)
	{
		if (cal(0) < 0 || cal(1) > 0)
			cout << "No solution" << endl;
		else
		{
			double x1 = 0, x2 = 1;
			while (fabs(x1 - x2) >= 1e-10)
			{
				double x = (x1 + x2)/2;
				if (cal(x) > 0)
					x1 = x;
				else
					x2 = x;
			}
			cout << setprecision(4) << fixed << x1 << endl;;
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值