回复 5楼 邓士林
我编制的程序如下,分成多少个晶格用l来控制。感觉有点问题,谢谢大侠指点一下问题何在:#include<stdio.h>
#include<math.h>
void main()
{
int i,j,k,l;
float vol,er;
scanf("%d",&l);
vol=0;
for(i=0;i<=l;i++)
for(j=0;j<=l;j++)
for(k=0;k<=l;k++)
{
if(i*i+j*j+k*k<=l*l)
{
if((i+0.5)*(i+0.5)+(j+0.5)*(j+0.5)+k*k<=l*l||(i+0.5)*(i+0.5)+j*j+(k+0.5)*(k+0.5)<=l*l||i*i+(j+0.5)*(j+0.5)+(k+0.5)*(k+0.5)<=l*l)
{
if(i!=0&&j!=0&&k!=0)
vol=vol+8*(18.00000/l)*(18.00000/l)*(18.00000/l);
else
{
if((i==0&&j!=0&&k!=0)||(i!=0&&j==0&&k!=0)||(i!=0&&j!=0&&k==0))
vol=vol+4*(18.00000/l)*(18.00000/l)*(18.00000/l);
else
{
if((i==0&&j==0&&k!=0)||(i==0&&j!=0&&k==0)||(i!=0&&j==0&&k==0))
vol=vol+2*(18.00000/l)*(18.00000/l)*(18.00000/l);
else
vol=vol+(18.00000/l)*(18.00000/l)*(18.00000/l);
}
}
}
else
{
if(i==l||j==l||k==l)
vol=vol+0.5*2*(18.00000/l)*(18.00000/l)*(18.00000/l);
else
{
if(i!=0&&j!=0&&k!=0)
vol=vol+8*0.67*(18.00000/l)*(18.00000/l)*(18.00000/l);
else
{
if((i==0&&j!=0&&k!=0)||(i!=0&&j==0&&k!=0)||(i!=0&&j!=0&&k==0))
vol=vol+4*0.67*(18.00000/l)*(18.00000/l)*(18.00000/l);
else
vol=vol+2*0.67*(18.00000/l)*(18.00000/l)*(18.00000/l);
}
}
}
}
}
er=fabs(vol-24429)/24429;
printf("计算体积=%f\n误差=%f\n",vol,er);
}