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

YT14-HDU-三分查找求F(x)的最小值

时间:2015-02-25 14:17:22      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:c++   iostream   namespace   编程   博客   

Problem 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

代码如下:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double Equ(double x,double Y)
{
    return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-Y*x;        //返回F(x)
}

int main()
{
    int T,i;
    double Y,low,high,t1,t2;
    cin>>T;
    while (T--)
    {
        cin>>Y;
        low=0,high=100;  
        for (i=1;i<=200;i++)                   //确定三分查找的次数
        {
            t1=(low+high)/2;
            t2=(t1+high)/2;                   //将0到100分为3份,逐渐缩小查找范围
            if (Equ(t1,Y)<Equ(t2,Y))
                high=t2;
            else
                low=t1;
        }
        cout<<setiosflags(ios::fixed)<<setprecision(4)<<Equ(t1,Y)<<endl;    //控制输出格式
    }
    return 0;
}


解题思路:

题目大意是输入Y,求F(x)在x属于【0,100】间的最小值。

用的是三分查找,二分查找也可以,但由于不确定F(x)在0到100间的单调性,所以还是三分查找更适用。




YT14-HDU-三分查找求F(x)的最小值

标签:c++   iostream   namespace   编程   博客   

原文地址:http://blog.csdn.net/liuchang54/article/details/43936155

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