HDU-Strange fuction-求函数的最小值

问题及代码:

Problem G Strange fuction

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size:  

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


/*    
*Copyright (c)2015,烟台大学计算机与控制工程学院    
*All rights reserved.    
*文件名称:HDU.cpp    
*作    者:单昕昕    
*完成日期:2015年2月27日    
*版 本 号:v1.0        
*/ 
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
double f(double x,long long y)
{
    return (42*x*x*x*x*x*x+48*x*x*x*x*x+21*x*x+10*x-y);
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        double mid=50.0,m=0.0,n=100.0,x,s;
        long long y;
        cin>>y;
        while(fabs(f(mid,y))>1e-5)
        {
            if((f(mid,y)>0))
            {
                n=mid;
                mid=(m+n)/2;
            }
            else if(f(mid,y)<0)
            {
                m=mid;
                mid=(m+n)/2;
            }
        }
        x=mid;
        s=6*x*x*x*x*x*x*x+8*x*x*x*x*x*x+7*x*x*x+5*x*x-y*x;
        printf("%.4lf\n",s);
    }
    return 0;
}


运行结果:

HDU-Strange fuction-求函数的最小值_第1张图片


知识点总结:


HDU-Strange fuction-求函数的最小值_第2张图片



学习心得:

如图。

数学问题,二次求导之后发现原函数单调递减,所以当一阶导数为0时的x值满足使原函数取得最小值。

即题目转化成求一阶导数为零的方程的解,然后带入原函数求得最小值。

你可能感兴趣的:(C++,HDU,fuction,strange,求函数的最小值)