将整个空间问题作个转化
草稿纸上列一下主要的几个式子
可以看出x^2的项都可以抵消,然后系数就显而易见了,
作好预处理后直接高斯消元做一遍即可
#include<bits/stdc++.h> using namespace std; const int N=101; double a[N][N]; int m,n; struct node { double x[12]; } pt[N]; double sqr(double x) {return x*x;} void prepare() { for (int i=2;i<=n+1;i++) { for (int j=1;j<=n;j++) { double xs=pt[i].x[j]-pt[i-1].x[j]; a[i-1][j]=2*xs; } double sum=0; for (int j=1;j<=n;j++) { sum+=sqr(pt[i].x[j]); sum-=sqr(pt[i-1].x[j]); } a[i-1][n+1]=sum; } } void solve() { for (int i=1;i<=n;i++) { int mx=i; for (int j=i+1;j<=n;j++) if (fabs(a[j][i])>fabs(a[mx][i])) mx=j; for (int j=1;j<=n+1;j++) swap(a[i][j],a[mx][j]); for (int j=1;j<=n;j++) if (j!=i) { double temp=a[j][i]/a[i][i]; for (int k=i+1;k<=n+1;k++) a[j][k]-=a[i][k]*temp; } } } int main() { scanf("%d",&n); for (int i=1;i<=n+1;i++) for (int j=1;j<=n;j++) scanf("%lf",&pt[i].x[j]); prepare(); solve(); for (int i=1;i<=n;i++) printf("%.3lf ",a[i][n+1]/a[i][i]); return 0; }