您的位置:首页 > 其它

Strange fuction

2015-07-23 10:17 399 查看
C - Strange fuction
Time Limit:1000MS Memory Limit:32768KB 64bit
IO Format:
%I64d & %I64u

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


这道题是求一个函数式的极值,可转化为求它导数等于0时的解,就变成了与前一道“Can
you solve this equation?”一样的模式。依然是二分法。
代码:
<span style="font-family:SimSun;font-size:14px;">#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int T;
double l,r,mid,x,Y,t=1e-8;
double v(double x){
return 42.0*pow(x,6) + 48.0*pow(x,5)+ 21.0*x*x + 10.0*x  ;
}
double vs(double x,double Y){
return 6 *pow(x,7)+8*pow(x,6)+7*x*x*x+5*x*x-Y*x; }
int main(){
cin>>T;
while(T--){
cin>>Y;
l=0; r=100;
if((v(0)-Y>0)||(v(100.0)-Y<0))
{
cout<<"No solution!"<<endl;
break;}
while(l<r){
mid=(l+r)/2;
if(fabs(v(mid)-Y)<t)
break;
else if(v(mid)-Y<0)
l=mid;
else
r=mid;
}
mid=(l+r)/2;
printf("%.4lf\n",vs(mid,Y));
}
return 0;
}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: