简单题意
给出公式,与结果,在0到100之间找出代入公式可以等于给定的值
解题思路形成过程
老师刚讲完二分算法肯定是用二分算法啦,要是用普通方法肯定会超时不断用二分法逼近函数值
感想
虽然百般小心,但还是超时了好几次,不断改细节终于AC了
AC代码
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;
double f(double x){
return (8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6);
}
int main(){
//ifstream cin("in.txt");
freopen("in.txt","r",stdin);
double low,high;
double m;
double y;
int t;
scanf("%d",&t);//cin>>t;
while(t--){
scanf("%lf",&y);//cin>>y;
if(f(0)>y||f(100)<y){
printf("No solution!\n");//cout<<"No solution!"<<endl;
continue;
}
low=0.0;high=100.0;m=50.0;
while(fabs(f(m)-y)>1e-5){
if(f(m)<y){
low=m;
m=(low+high)/2;
}else if(f(m)>y){
high=m;
m=(low+high)/2;
}
}
printf("%.4lf\n",m);//cout<<setiosflags(ios::fixed)<<setprecision(4)<<m+0.00005<<endl;
}
return 0;
}