训练2 习题2

来源:互联网 发布:哪个软件可以看素媛 编辑:程序博客网 时间:2024/06/01 09:52

题目: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.

思路:求导    又有实际意义知极值为0   是解  用二分法求x    然后求F(x)!


代码:# 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;
}
0 0
原创粉丝点击