这道题的算法是将大小不同的圆环交叉放置,最大与最小的放在一起,也就是说体积差异最大放在一起。 // How Big Is It.cpp : Defines the entry point for the console application. // #include "stdafx.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { double CalLength(double r1,double r2); int n=0; cin>>n; while(n>0) { int m=0; double L=0; cin>>m; double *RT=new double[m]; for(int i=0;i<m;++i)cin>>RT[i]; if(m==1) { L=RT[0]*2; cout<<L<<"/n"; --n; continue; } else { for(int x=1;x<=m-1;++x) { for(int y=0;y<m-1;++y) { if(RT[y]>RT[y+1]) { double tmp=RT[y]; RT[y]=RT[y+1]; RT[y+1]=tmp; } } } if(m%2!=0) { L=RT[0]+RT[m/2]; int x,y; for(x=0,y=m-1;x<=m/2-1,y>=m/2+1;++x,--y) { L=L+CalLength(RT[x],RT[y]); if(y==m/2-1) { L=L+CalLength(RT[m/2],RT[m/2+1]); break; } else { double R1=RT[x+1]; L=L+CalLength(R1,RT[y]); } } cout<<L<<"/n"; } else { L=RT[0]; int x,y; for(x=0,y=m-1;x<=m/2-1,y>=m/2;++x,--y) { L=L+CalLength(RT[x],RT[y]); if(y==m/2) { L=L+RT[y]; break; } else { double R1=RT[x+1]; L=L+CalLength(R1,RT[y]); } } cout<<L<<"/n"; } --n; delete []RT; } } system("pause"); return 0; } double CalLength(double r1,double r2) { double R=abs(r1+r2); double r=abs(r1-r2); R=R*R; r=r*r; return sqrt(R-r); }