高斯消元裸题,设球的一般方程为a^2+b^2+c^2+...+z^2+Aa+Bb+Cc+...+Zz=常数,求解即可code:
球心坐标为(A/-2,B/-2,C/-2,...,Z/-2)
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; double a[15][15],b[15],x[15]; int n; int main() { int i,j,k; double xi,bz; scanf("%d",&n); for (i=1;i<=n+1;++i) for (j=1;j<=n;++j) { scanf("%lf",&xi); a[i][j]=xi; a[i][n+1]=1; b[i]-=xi*xi; } for (k=1;k<=n+1;++k) { if (a[k][k]==0) { for (j=k+1;j<=n+1;++j) if (a[j][k]!=0) { for (i=1;i<=n+1;++i) swap(a[j][i],a[k][i]); swap(b[j],b[k]); break; } } for (j=k+1;j<=n+1;++j) { bz=a[j][k]/a[k][k]; for (i=1;i<=n+1;++i) a[j][i]-=a[k][i]*bz; b[j]-=b[k]*bz; } } for (k=n+1;k>=1;--k) { x[k]=b[k]/a[k][k]; for (j=k-1;j>=1;--j) { b[j]-=x[k]*a[j][k]; a[j][k]=0; } } for (i=1;i<=n-1;++i) { xi=-x[i]/2; printf("%.3f ",xi); } xi=-x[n]/2; printf("%.3f\n",xi); }