/*Strange fuction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3102 Accepted Submission(s): 2292
Problem Description
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
Author
Redow
*/
#include<stdio.h>
double Y;
double f(double x)
{
return 6*x*x*x*x*x*x*x + 8*x*x*x*x*x*x + 7*x*x*x + 5*x*x - Y*x;
}
double ans()
{
double a = 0, b = 100, x, y;
while(b - a > 1e-6)//误差允许范围
{
x = a + (b-a) / 3;
y = a + (b - a) / 3 * 2;
if( f(x) > f(y))//每次挑出最大的直至接近最小值
a = x;
else
b = y;
}
return f(a);
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%lf", &Y);
printf("%.4lf\n", ans());
}
return 0;
}
题意:给出一个函数f(x),其中还有一个未知系数y,输入y,求当x属于区间[0,100]时f(x)的最小值。(y>0)
思路:由于y>0&x>0,所以根据函数的求导可以发现最小值必然存在于(0,+∞)之间,然后我们就可以确定三分法必定可以取到最小值,每次更新区间函数对应的最大值,使得最大值越来越趋近于最小值,当误差小于题目要求的范围时即可输出最小值。
注意:double误差:
难点:要掌握三分法求方程解