算法-初次尝试-模拟退火
作者:互联网
我对模拟退火的初步理解,还没深入了解过。这里只是用模拟退火求函数极值。
题目:https://vjudge.net/problem/HDU-2899
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
const ll MAXN=1e18;
const double eps=1e-8;
double y;
double func(double x){
return 6*x*x*x*x*x*x*x+8*x*x*x*x*x*x+7*x*x*x+5*x*x-y*x;
}
int f[2]={-1,1};
int main(){
srand(time(NULL));
int t;
cin>>t;
while(t--){
cin>>y;
double T=100;
double delta=0.98;
double x=50;
double ans=func(x);
while(T>eps){
int f[2]={-1,1};
double newx=x+f[rand()%2]*T;
if(newx>=0&&newx<=100){
double next=func(newx);
if(ans>next){
ans=next;
x=newx;
}
}
T*=delta;
}
cout<<fixed<<setprecision(4)<<ans<<'\n';
}
return 0;
}
标签:int,double,newx,long,cin,算法,模拟退火,初次 来源: https://www.cnblogs.com/Flowyuan-Foreverworld/p/14674357.html