【bzoj1013】 JSOI2008—球形空间产生器sphere
www.lydsy.com/JudgeOnline/problem.php?id=1013 (题目链接)
题意
有一个n维的球体,给出球上n+1个点,求出圆心。
Solution
题中给出了对于n维空间点与点之间的距离求法。那么我们将圆心的坐标设为{x1,x2,x3……xn},那么就可以列出n个n元一次方程。
高斯消元。
代码
// bzoj1013 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #include<queue> #define LL long long #define inf 2147483640 #define Pi acos(-1.0) #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout); using namespace std; const int maxn=30; double f[maxn],a[maxn][maxn]; int n; void Gauss() { for (int r,i=1;i<=n;i++) { r=i; for (int j=i+1;j<=n;j++) if (fabs(a[r][i])<fabs(a[j][i])) r=j; if (a[r][i]==0) continue; if (r!=i) for (int j=1;j<=n+1;j++) swap(a[i][j],a[r][j]); for (int j=1;j<=n;j++) if (j!=i) { for (int k=n+1;k>=i;k--) a[j][k]-=a[j][i]/a[i][i]*a[i][k]; } } } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%lf",&f[i]); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) { double t;scanf("%lf",&t); a[i][j]=2*(t-f[j]); a[i][n+1]+=t*t-f[j]*f[j]; } Gauss(); for (int i=1;i<n;i++) printf("%.3lf ",a[i][n+1]/a[i][i]); printf("%.3lf",a[n][n+1]/a[n][n]); return 0; }
This passage is made by MashiroSky.