地址:http://acm.hdu.edu.cn/showproblem.php?pid=2899
Now, here is a fuction:
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
#include<bits/stdc++.h> #include<iostream> #include<cstring> #include<cmath> #include<cstdlib> #include<stack> #include<queue> #include<cstdio> #include<vector> #include<algorithm> using namespace std; typedef long long ll; 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)-x*y; } double solve() { double T=100; //初始温度 double delta=0.98;//降温系数 double x=50.0; //x的初始值 double now=func(x); //初始函数值 double ans=now; //记录结果 while(T>eps) { int f[2]={1,-1}; double newx=x+f[rand()%2]*T; //按概率改变x if(newx>=0&&newx<=100) { double next=func(newx); ans=min(ans,next); if(now-next>eps) { x=newx; now=next; } // cout<<x<<endl; } T*=delta; } return ans; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%lf",&y); printf("%.4lf ",solve()); } }