莫欺少年穷,修魔之旅在这开始—>算法提高课题解
$设球心坐标\ (x_1,x_2,…,x_n),第\ i\ 个点坐标\ (a_{i1},a_{i2},…,a_{in}),半径为\ R$
$易得方程组\begin{cases}(a_{11}-x_1)^2+(a_{12}-x_2)^2+…+(a_{1n}-x_n)^2=R^2\\\\(a_{21}-x_1)^2+(a_{22}-x_2)^2+…+(a_{2n}-x_n)^2=R^2\\\\\vdots\\\\(a_{{n+1}1}-x_1)^2+(a_{{n+1}2}-x_2)^2+…+(a_{{n+1}n}-x_n)^2=R^2\end{cases}$
$第\ i+1\ 个方程-第\ i\ 个方程:\\\\2(a_{{i+1}1}-a_{i1})x_1+2(a_{{i+1}2}-a_{i2})x_2+…+2(a_{{i+1}n}-a_{in})x_n=(a_{{i+1}1}^2-a_{i1}^2)+(a_{{i+1}2}^2-a_{i2}^2)+…+(a_{{i+1}n}^2-a_{in}^2)$
$设\ b_{ij}=2(a_{{i+1}j}-a_{ij})\ (1\leqslant i,j \leqslant n),b_{i{n+1}}=(a_{{i+1}1}^2-a_{i1}^2)+(a_{{i+1}2}^2-a_{i2}^2)+…+(a_{{i+1}n}^2-a_{in}^2)\ (1\leqslant i \leqslant n)$
$则得方程组\begin{cases}b_{11}x_1+b_{12}x_2+…+b_{1n}x_n=b_{1{n+1}}\\\\b_{21}x_1+b_{22}x_2+…+b_{2n}x_n=b_{2{n+1}}\\\\\vdots\\\\b_{n1}x_1+b_{n2}x_2+…+b_{nn}x_n=b_{n{n+1}}\end{cases}$
$最后运用高斯消元求解即可$
可参考: 高斯消元解线性方程组
#include<bits/stdc++.h>
using namespace std;
const int N = 15;
int n;
double a[N][N],b[N][N];
int main()
{
cin>>n;
for(int i=1;i<=n+1;i++)
for(int j=1;j<=n;j++)
cin>>a[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
b[i][j]=(a[i+1][j]-a[i][j])*2;
b[i][n+1]+=a[i+1][j]*a[i+1][j]-a[i][j]*a[i][j];
}
for(int r=1,c=1;c<=n;r++,c++)
{
int t=r;
for(int i=r+1;i<=n;i++)
if(fabs(b[i][c])>fabs(b[t][c]))
t=i;
for(int i=c;i<=n+1;i++) swap(b[t][i],b[r][i]);
for(int i=n+1;i>=c;i--) b[r][i]/=b[r][c];
for(int i=r+1;i<=n;i++)
for(int j=n+1;j>=c;j--)
b[i][j]-=b[i][c]*b[r][j];
}
for(int i=n-1;i>0;i--)
for(int j=i+1;j<=n;j++)
b[i][n+1]-=b[i][j]*b[j][n+1];
for(int i=1;i<=n;i++) printf("%.3lf ",b[i][n+1]);
return 0;
}