题目描述
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-5,如果精度太高的话会超时的
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<iomanip>
using namespace std;
double m, res = 0;
int ji = 0;
double f(double res)
{
return res*res*res*res * 8 + res*res*res * 7 + res*res * 2 + res * 3 + 6;
}
int main()
{
//cin.sync_with_stdio(false);
//freopen("date.in", "r", stdin);
//freopen("date.out", "w", stdout);
int N, temp;
double b, e, tem = 50;
scanf("%d", &N);
for (int i = 0; i<N; i++){
b = 0, e = 100, tem = 50;
cin >> m;
if (m<6 || m>807020306)
//cout << "No solution!" << endl;
printf("No solution!\n");
else{
while (fabs(f(tem) - m) >= 1e-5)
if (f(tem)>m){
e = tem;
tem = (b + e) / 2;
}
else{
b = tem;
tem = (b + e) / 2;
}
//cout << fixed << setprecision(4) << tem << endl;
printf("%0.4f\n", tem);
}
}
}