Strange fuction






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.
InputThe 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) OutputJust 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


题意:输入一个Y,求方程的最小解。

题解:用三分法,注意精度。

AC代码为;

#include<iostream>
#include<cmath>
#include<cmath>
#include<algorithm>


using namespace std;


double f(double x,double y)
{
return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
}


int main()
{
int T;
double y;

cin>>T;

while(T--)
{
cin>>y;
double t1,t2,mid1,mid2;
t1=0,t2=100;
for(int i=0;i<200;i++)
{
mid1=(t1+t2)/2;
mid2=(mid1+t2)/2;

if(f(mid1,y)>f(mid2,y))
{
t1=mid1;
}
else
{
t2=mid2;
}

}
printf("%.4lf\n",f(mid1,y));
}

return 0;
}


posted @ 2018-01-23 20:54  StarHai  阅读(279)  评论(0编辑  收藏  举报