Strange fuction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4794 Accepted Submission(s): 3419
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.
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,y,然后给出 x 的范围和 y 的值,让你求出 x 取得何值时,该方程的值最小,x 的精度精确到小数点后四位...
典型的数学题目,y 的值已经知道,当成常数,为了求得整个函数的最小值,而且函数并不一定单调,那么找最值点就先对这个方程求导函数,极值处取得的点肯定就是
最值,如果没有极值,那么就取端点值就可以了.......
谈论上数学了...其实这个题的做法也是,一直二分逼近,最后求得的就是需要的 x 值,代入函数求出函数的值.....
ps:刚刚比赛的一道题,因为多打了一个头文件,然后提交方式选错了,ce 了,那个伤心啊~
#include<stdio.h>
double fun(double x,double y)//方程的导函数
{
return (42*x*x*x*x*x*x+48*x*x*x*x*x+21*x*x+10*x)>y; //判断正负
}
double fun1(double x,double y)//原方程
{
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 abs(double x)//绝对值,自己构造也可以
{
if(x<0)
{
x=-x;
}
return x;
}
int main()
{
int t;double y;
scanf("%d",&t);
while(t--)
{
scanf("%lf",&y);
double l=0,r=100,mid,tp;
while(abs(l-r)>1e-6)//精度限定
{
mid=(l+r)/2.0;//二分法查找
tp=fun(mid,y);
if(tp)//极值点处导函数为零,所以选择在零附近逼近..
{
r=mid;
}
else
{
l=mid;
}
}
printf("%.4lf\n",fun1(l,y));//计算结果...
}
return 0;
}