hdu2899 Strange fuction (二分&三分)


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2899


#include <stdio.h>
#include <math.h>

double func1(double x)
{//一次求导,func1递增
	return 42*pow(x,6)+48*pow(x,5)+21*x*x+10*x;
}

double func2(double x,int y)
{
	return 6*pow(x,7)+8*pow(x,6)+7*x*x*x+5*x*x-y*x;
}

int main()
{
	int test,y;
	double right,left,mid;
	scanf("%d",&test);
	while(test--)
	{
		scanf("%d",&y);
		if(func1(100.0)-y<=0)//判断函数单调性
		{
			printf("%.4lf\n",func2(100.0,y));
			continue;
		}
		left=0.0;//函数的二次导数得函数为凹函数,有最小值
		right=100.0;
		while(right-left>=1e-8)
		{
			mid=(left+right)/2.0;
			if(func1(mid)-y<0)
				left=mid;
			else
				right=mid;
		}
		printf("%.4lf\n",func2(mid,y));
	}
	return 0;
}



#include <stdio.h>
#include <math.h>

double func(double x,double y)
{
	return 6*pow(x,7)+8*pow(x,6)+7*x*x*x+5*x*x-y*x;
}

int main()
{
	int test;
	double left,right,leftThrid,rightThrid,y;
	scanf("%d",&test);
	while(test--)
	{
		scanf("%lf",&y);
		left=0.0;
		right=100.0;
		while(right-left>=1e-8)
		{
			leftThrid=(2*left+right)/3.0;
			rightThrid=(left+2*right)/3.0;
			if(func(leftThrid,y)>func(rightThrid,y))
				left=leftThrid;
			else
				right=rightThrid;
		}
		printf("%.4lf\n",func(left,y));
	}
	return 0;
}



你可能感兴趣的:(hdu2899 Strange fuction (二分&三分))