您的位置:首页 > 其它

Strange fuction

2016-04-19 22:42 323 查看
[align=left]Problem Description[/align]
Now, here is a fuction:<br>  F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)<br>Can you find the minimum value when x is between 0 and 100.

[align=left]Input[/align]
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)

[align=left]Output[/align]
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.

[align=left]Sample Input[/align]

2
100
200

[align=left]Sample Output[/align]

-74.4291
-178.8534

题意:求F(x)最小值
思路:一开始没有思路,然后参考了一下网上的思路,可以先求f',令f'=0,通过二分得出y与x的关系,有式子function(x0)= y,发现当x < x0,F(x)单调递减,当x > x0,F(x)单调递增,从而得到F(x)的最小值

代码:
#include <iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
const double eps = 1e-6;
double cal(double x){
  return 42*pow(x,6.0)+48*pow(x,5.0)+21*pow(x,2.0)+10*x;
}
double get(double x,double y){
  return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-x*y;;
}
int main()
{
  int T = 0;
  double Y = 0.0;
  double x = 0.0;
  cin >> T;
  while(T--){
    cin >> Y;
    double res = 0.0;
    double low = 0.0;
    double high = 100.0;
    while(high - low > eps){
      x = (high + low)/2;
      res = cal(x);
      if(res < Y){
        low = x + 1e-8;
      }
      if(res > Y){
        high = x - 1e-8;
      }
    }
    double value = get(x,Y);
    printf("%.4f\n",value);
  }
  return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: