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

HDU 搜索练习 Strange fuction

时间:2016-04-13 15:54:53      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

Strange fuction

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 97 Accepted Submission(s) : 49
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
 
简单题意:
  给出方程,求解x取0 - 100时的最小值。
思路:
  数学问题,先进行求导,当导数为0时则为极值。
 
# include <iostream>
# include <cmath>
using namespace std;
double f(double x, double y)
{
    double fx = 42 * pow(x, 6) + 48 * pow(x, 5) + 21 * pow(x, 2) + 10 * x - y;
    return fx;
}
double f1(double x, double y)
{
    double fx = 6 * pow(x, 7) + 8 * pow(x, 6) + 7 * pow(x, 3) + 5 * pow(x, 2) - y * x;
    return fx;
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        double y;
        cin >> y;
        double begin = 0, end = 100, mid;
        int i = 0;
        while(1)
        {
            mid = (begin + end) / 2;
            if(fabs(f(mid, y)) <= 0.0001)
            {
                cout.precision(4);
                cout << fixed <<  f1(mid, y) << endl;
                break;
            }
            else if(f(mid, y) > 0)
            {
                end = mid;
            }
            else if(f(mid, y) < 0)
            {
                begin = mid;
            }
        }

    }
    return 0;
}

 

HDU 搜索练习 Strange fuction

标签:

原文地址:http://www.cnblogs.com/lyf-acm/p/5387439.html

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