码迷,mamicode.com
首页 > 其他好文 > 详细

Strange fuction

时间:2016-04-20 00:18:47      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description
Now, here is a fuction:<br>&nbsp;&nbsp;F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 &lt;= x &lt;=100)<br>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
 
题意:求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;
}

Strange fuction

标签:

原文地址:http://www.cnblogs.com/2016zhanggang/p/5410548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!