hdu 2899 Strange fuction (模拟退火)

题目链接

题目大意

给你一个函数,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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值