题目大意:
给出一个方程,8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 =Y,和得数Y,求方程在0——100内的解x,保留四位小数;
解题思路:
很明显该函数为单增函数,用二分搜索,low为0,high为1,mid为(low+high).2,若比Y小,low取mid,反之high取mid,再用iomanip头文件实现精度控制;
代码:
#include <iostream> #include <cmath> #include <iomanip> using namespace std; double get(double mid) { return 8*pow(mid,4)+7*pow(mid,3)+2*pow(mid,2)+3*mid+6; } int main() { int T; double mid,low,high,Y; cin>>T; while(T--) { cin>>Y; if(Y<get(0)||Y>get(100)) { cout<<"No solution!"<<endl; continue; } low=0; high=100; while(high>low+0.00000001) { mid=(low+high)/2; if(get(mid)<Y) low=mid+0.00000001; else high=mid-0.00000001; } cout<<setiosflags(ios::fixed)<<setprecision(4)<<mid<<endl; } return 0; }