题目大意
给你一个函数,F(x) = 6 * x7+8*x6+7x3+5*x2-yx (0 <= x <=100)
输入y值,求函数的最小值,结果保留四位小数
思路
直接模拟退火暴力(
代码
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps = 1e-8;
double y;
double func(double x) {
return 6 * pow(x, 7.0) + 8 * pow(x, 6.0) + 7 * pow(x, 3.0) + 5 * pow(x, 2.0) - y * x;
}
double solve() {
double temperature = 100; //初始温度
double delta = 0.98; //降温系数
double x = 50; //x初值
double now = func(x); //函数初值
double ans = now; //记录最优结果
while (temperature > eps) { //降温到零度,也就是eps,结束
int f[2] = {1, -1};
double x_new = x + f[rand() % 2] * temperature;
if (x_new >= 0 && x_new <= 100) {
double next = func(x_new);
ans = min(ans, next);
if (now - next > eps)
x = x_new, now = next;
}
temperature *= delta; //降温
}
return ans;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
cin >> y;
cout << fixed << setprecision(4) << solve() << '\n';
}
return 0;
}