目录
题目描述:
Now, here is a function:
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.
翻译:
现在,这是一个函数:
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
当 x 介于 0 和 100 之间时,你能找到最小值吗?
输入:
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)
翻译:
输入的第一行包含一个整数 T(1<=T<=100),表示测试用例的数量。然后T线跟随,每条线只有一个实数Y 。(0<Y<1e10)
输出:
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
翻译:
当 x 介于 0 和 100 之间时,只有最小值(精确到小数点后 4 位)。
输入样例:
2
100
200
输出样例:
-74.4291 -178.8534
提示:
这题求的是函数曲线最低点的值,可以用三分查找来实现。
代码:
#include<stdio.h>
#include<math.h>
double bas=1e-6;//设置范围值
double f(double x,double y){
double res;
res=6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
return res;
}
int main(){
int t;
scanf("%d",&t);
for(int i=0;i<t;i++){
double n,y,l,h,m,mm;
scanf("%lf",&y);
l=0.0;
h=100.0;
while(h-l>bas){
m=(h+l)/2;
mm=(h+m)/2;
if(f(m,y)>f(mm,y))
l=m;//注意题目的要求是求最低点
else
h=mm;
}
printf("%.4lf\n",f(m,y));
}
return 0;
}