题目:
Now, here is a fuction:
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
Sample Input
2 100 200
Sample Output
-74.4291 -178.8534
题意:告知我们 x 的范围,求解函数式的最小值。
解题思路:标准三分模板,cal函数设置为函数式即可。
ac代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#define maxn 10000
#define eps 1e-8
using namespace std;
double ksm(double a,int b)
{
double t=1;
while(b)
{
if(b&1)
t*=a;
a*=a;
b>>=1;
}
return t;
}
double cal(double y,double x)
{
return 6*ksm(x,7)+8*ksm(x,6)+7*ksm(x,3)+5*x*x-y*x;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double y;
scanf("%lf",&y);
double rmax,rmin,x;
rmin=0.0,rmax=100.0;
while(rmax-rmin>eps)
{
double mid=rmin+(rmax-rmin)/3;
double midd=rmax-(rmax-rmin)/3;
if(cal(y,mid)<cal(y,midd)) rmax=midd;
else rmin=mid;
}
double ans=cal(y,rmin);
printf("%.4lf\n",ans);
}
return 0;
}