E - Can you solve this equation?

Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);
Output
For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.
Sample Input
2
100
-4
Sample Output
1.6152
No solution!

题解:
上述方程在给定范围内单调递增,符合二分的条件,对区间内的数进行枚举,找解
此题可能无法求出具体值,所以要尽可能控制精度(题目也要求了只需保留四位有效数字)我把精度逐个试了一遍,发现1e-6就已经足够ac了
除了精度问题,此题就没有坑了
但是我自己人为的挖出了一个坑
就是做题中我习惯性的加上了

std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);//以此来关闭同步流

来关闭同步流,结果wa了十多次也没把题目做出来
wa到我怀疑人生的时候
我随便把关闭同步流的代码给注释后,就意外的ac了,,,,笑哭(原理我并不清楚,希望有大佬可以帮帮我)

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#define Precison 1e-8
using namespace std;
typedef long long ll;

double func(double x) {
	return 8.0 * x * x * x * x + 7.0 * x * x * x + 2.0 * x * x + 3.0 * x + 6.0;
}
int main() {
	//std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);不能把这个加上
	int T;
	cin >> T;
	while (T--) {
		double y;
		cin >> y;
		if (y < 6.0 || func(100.0) < y) {
			cout << "No solution!" << endl;
			continue;
		}
		int cnt = 50;
		double left = 0.0;
		double right = 100.0;
		double mid = 0.0;
		while (right-left>1e-6) {
			mid =(left+right)/2.0;
			if (func(mid)==y)
				break;
			if (func(mid) > y) {
				right = mid;
			}
			else{
				left = mid;
			}
		}
		printf("%.4f\n", mid);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

veit sun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值