hdu 2899 Strange fuction

与之前的2199类似,只是求完一次导后发现并非完全单调,求二次导,恒大于0,说明图形是凹的。已知 f ' (0) <0,接着判断f ' (100),若也<0,说明从0到100都是单调递减的,f(100)是最小值。若>0,则用二分法求出f ' (x) =0 时的x值,则最小值就是f(x)。

#include<stdio.h>
#include<math.h>
double cal(double x,double y)
{
	return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
}

double qiudao(double x)
{
	return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x;
}

int main()
{
	int t;
	double y,left,right,mid;
	scanf("%d",&t);
	while(t--){
		scanf("%lf",&y);
		if(qiudao(100)-y<=0){
			printf("%.4lf\n",cal(100,y));
			continue;
		}
		left=0;
		right=100;
		while(right-left>1e-6){
			mid=(left+right)/2;
			if(qiudao(mid)-y<0)
				left=mid+1e-6;
			else
				right=mid-1e-6;

		}
		printf("%.4lf\n",cal((left+right)/2,y));

		
	}
	return 0;

}


 

你可能感兴趣的:(hdu 2899 Strange fuction)