hdu2899(三分)函数最小值

版权声明:仅供研究,转载请注明出处。 https://blog.csdn.net/CSUstudent007/article/details/82377581

 观察函数知这是个单峰函数,直接三分。

注意一点:1.eps开小些,2.x是[0,100]的实数(我一开始以为y是实数,x是整数,结果WA)。

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
const double eps = 1e-12;
double f(double x,double y){
	return  6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
}
double bsearch(double y){
	double left=0,right=100;
	double midl,midr;
	while(left+eps<right){
		midl=(left+right)/2;
		midr=(midl+right)/2;
		if(f(midl,y)<f(midr,y)){
			right=midr;
		}
		else{
			left=midl;
		}
	}
	return f(left,y);
}
int main()
{
	int t;
	double y;
	cin>>t;
	while(t--){
		cin>>y;
		double c=bsearch(y);
		printf("%.4lf\n",c);
	}
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/CSUstudent007/article/details/82377581
今日推荐