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;
}