三分_1

来源:互联网 发布:unity3d 积木游戏 编辑:程序博客网 时间:2024/05/16 18:58

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

2100200

Sample Output

-74.4291-178.8534

三分题。。二分应用单调函数,而三分应用于单峰函数,此函数恰是一个单峰函数。这题可以直接用三分,当然也求导求极值,从而转化为二分题

#include<iostream>#include"math.h"#include"cstdio"typedef long long LL;const double EPS=1e-7;LL y;using namespace std;//F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)double f(double x){  return 6*x*x*x*x*x*x*x+8*x*x*x*x*x*x+7*x*x*x+5*x*x-y*x;}int main(){    int T;    cin>>T;    while(T--)    {        cin>>y;        double low=0.0,high=100.0;        double mid,mmid;        while(high-low>EPS)        {            mid=(high-low)/2+low;            mmid=(high-mid)/2+mid;            if(f(mid)<f(mmid))  high=mmid;            else low=mid;        }        printf("%.4lf\n",f(mid));    }    return 0;}


0 0