Description
给出一个函数F(x)=6*x^7+8*x^6+7*x^3+5*x^2-y*x(0<=x<=100),现给出y值,输出F(x)最小值
Input
第一行为一个整数t表示用例组数,每组用例占一行为一个整数y(0 < y < 1e10)
Output
对于每个y值,输出F(x)在区间[0,100]上的最小值,结果保留到小数点后四位
Sample Input
2
100
200
Sample Output
-74.4291
-178.8534
Solution
对F(x)求导后得到其导函数G(x)=42*x^6+48*x^5+21*x^2+10*x-y,显然G(x)在[0,100]上递增,当G(x)有零点时,二分求其零点x0,F(x0)即为最小值;当G(x)无零点,即G(100)<=0时,F(100)即为最小值
Code
#include<stdio.h>
#include<math.h>
#define eps 1e-8
int y;
double g(double x)
{
return 42.0*pow(x,6)+48.0*pow(x,5)+21.0*pow(x,2)+10.0*x;
}
double f(double x)
{
return 6.0*pow(x,7)+8.0*pow(x,6)+7.0*pow(x,3)+5.0*pow(x,2)-y*x;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&y);
if(g(100.0)-y<eps)//f(x)递减
{
printf("%.4lf\n",f(100.0));
continue;
}
double l=0,r=100.0,mid;
while(r-l>=eps)//二分求g(x)零点
{
mid=(l+r)/2.0;
if(g(mid)-y<eps) l=mid;
else r=mid;
}
printf("%.4lf\n",f(mid));
}
return 0;
}