概述:给定一个函数 Z=f(x,y),单调性未知,给定y,求Z的最小值
思路:对函数求导得到单调性,根据单调性确定取最小值时的条件,做的时候因为不明白函数的单调性,所以要先求导,求导之后,明确了单调性和取极值的条件,就可以用二分求极值点了,这里需要一点高中数学的知识。
感受:做这道题的时候要学会转变思路,对于单调性不明的函数要求导。
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
inline double f(double x, double y)
{
return 6 * pow(x, 7) + 8 * pow(x, 6) + 7 * pow(x, 3) + 5 * pow(x, 2) - y*x;
}
inline double df(double x, double y)
{
return 42 * pow(x, 6) + 48 * pow(x, 5) + 21 * pow(x, 2) + 10 * x - y;
}
int main()
{
int n;
double y;
cin >> n;
while (n--)
{
cin >> y;
double x1 = 0, x2 = 100;
while (x2 - x1 >= 1e-6)
{
if (df((x1 + x2) / 2, y)>0)
x2 = (x1 + x2) / 2;
else if (df((x1 + x2) / 2, y) == 0)
break;
else
x1 = (x1 + x2) / 2;
}
printf("%0.4lf\n", f((x1 + x2) / 2, y));
}
return 0;
}
1658

被折叠的 条评论
为什么被折叠?



