模拟退火模板,按比例接受
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; const double eps=1e-8; double ans; double y; double cal(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;} int f[2]={1,-1}; double solve(double tmp){ double res=1e18; double cur; double sx=50.0; cur=cal(sx); double dc=0.98; res=min(res,cur); while(tmp>eps){ double nx=sx+f[rand()%2]*tmp; if(nx>100.0||nx<0.0); else{ double nxd=cal(nx); res=min(res,nxd); if(cur-nxd>eps)sx=nx,cur=nxd; else if(exp(-(nxd-cur)/tmp)*RAND_MAX>rand()){ sx=nx,cur=nxd; } } tmp*=dc; } return res; } int main(){ //ios::sync_with_stdio(false); int t; cin>>t; while(t--){ cin>>y; ans=solve(100); printf("%.4f\n",ans); } }View Code