您的位置:首页 > 编程语言 > C语言/C++

HDU-2899

2017-02-09 08:52 155 查看
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
这道题在做的时候,最开始觉得这个函数应该是用三分法解决,但是用了之后,会发现怎么都ac不了,都是
wa,然后在网上查完资料之后,发现这道题就应该是先求导,之后再根据导数的正负,来运用二分法解决问
题,因为我们要求的是一个最值而跟极值的概念还是有不同的,之后将过程实现,就可以ac了,这道题感觉
就是可以运用数学知识解决问题
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;

double cal(double x,double y)
{
//6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
return(6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x);
}

double cal_d(double x,double y)
{
return(42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x-y);
}

int main()
{
int times;
cin>>times;
while(times--)
{
long long y_get;
cin>>y_get;
double low = 0,high = 100;
double mid;
while(low<high-0.000001)
{
mid = (low+high)/2;
if(cal_d(mid,y_get)>0) high = mid;
else low = mid;
}
printf("%.4f\n",cal(mid,y_get));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 二分法 acm