acm_1002_Strange fuction

来源:互联网 发布:胡歌的电视剧 知乎 编辑:程序博客网 时间:2024/06/05 02:22

题目描述:

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<br>100<br>200
 

Sample Output
-74.4291<br>-178.8534
 

Author
Redow
 

题目大意:

就是给你一个函数,函数有两个变量,把Y的值给你,让你求x处于(0,100)内函数的最小值。。


想法:

可以把这个函数先求导,得到一个含有Y单值 + 一个含有X的函数体,

1:把输入的Y值和X=100是函数体得到的值比较,如果大,说明函数是递减的,最小值是x处于100时;若否,则->2。。

2:把输入的Y值和X=0是函数体得到的值比较,如果小,说明函数是递增的,最小值是x处于0时;若否,则->3。。

3:当函数体=y时,求出X的值,可用二分查找,带入原函数即可。。


代码:

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
    int Y,T;
    double min,sum,max,flat;
    cin>>T;
    for(int i=1;i<=T;i++)
    {
        cin>>Y;
        if((42*pow(100,6)+48*pow(100,5)+21*10000+10*100)<Y)
        {
            sum=6*pow(100,7)+8*pow(100,6)+7*pow(100,3)+5*10000-100*Y;
            cout<<setprecision(4)<<fixed<<sum<<endl;
        }
        else if(0>Y)
        {
            sum=0;
            cout<<setprecision(4)<<fixed<<sum<<endl;
        }
        else
        {
            max=100.0;
            min=0.0;
            while((max-min)>0.0000001)
            {
                flat=(max+min)/2;
                if((42*pow(flat,6)+48*pow(flat,5)+21*pow(flat,2)+10*flat)<Y)
                    min=flat;
                else max=flat;
                if((max-min)<0.00001)
                {
                    sum=6*pow(flat,7)+8*pow(flat,6)+7*pow(flat,3)+5*pow(flat,2)-flat*Y;
                    cout<<setprecision(4)<<fixed<<sum<<endl;
                    break;
                }
            }
        }
    }
    return 0;
}

0 0
原创粉丝点击