一步一步慢慢学习~~~
模拟退火大法好~~~
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
const int N = 30 +5;
double a,b,c,d,e,f;
double dist(double x,double y,double z)
{
return sqrt(x*x+y*y+z*z);
}
struct point
{
double x;double y;double z;
point() {}
point(double a,double b,double c):x(a),y(b),z(c) {}
double dis() {return dist(x,y,z);}
}p[N],pp[N];
int mox[]={-1,-1,-1,0,0 ,1 ,1,1};
int moy[]={-1,1 ,0 ,1,-1,-1,1,0};
bool check(double x,double y)
{
double A = c;
double B = d*y + e*x;
double C = f*x*y + a*x*x + b*y*y - 1;
if(B*B - 4*A*C < 0)
return 0;
else
return 1;
}
double getZ(double x,double y)
{
double A = c;
double B = d*y + e*x;
double C = f*x*y + a*x*x + b*y*y - 1;
double dd = sqrt(B*B - 4*A*C);
double z1 = (dd - B)/(2*A);
double z2 = (-dd - B)/(2*A);
if(dist(x,y,z1)<dist(x,y,z2))
return z1;
else
return z2;
}
int main()
{
while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f)!=EOF)
{
double ans = 1e20;
double T = 1;
double Tmin = 1e-8;
double r = 0.99;
p[1].x=0;p[1].y=0;p[1].z=sqrt(1.0/c);
while(T>Tmin)
{
for(int i=1;i<=1;i++)
{
double tx = p[i].x, ty = p[i].y;
for(int j=0;j<8;j++)
{
double xx = tx + T*mox[j];
double yy = ty + T*moy[j];
if(check(xx,yy))
{
double zz = getZ(xx,yy);
if(dist(xx,yy,zz) < p[i].dis())
{
point temp(xx,yy,zz);
p[i]=temp;
}
}
ans = min(ans,p[i].dis());
}
}
T*=r;
}
printf("%.7f\n",ans);
}
return 0;
}
代码写的有些屎~~~~,没办法,自己太渣。
这道题因为精度WA了很多次,这也是模拟退火的着重点,注意!!!