高斯消元模板 [BZOJ][1013][JSOI2008]球形空间产生器sphere

#include<bits/stdc++.h>
using namespace std;

#define MAXN 10+10

int n;
double a[MAXN][MAXN],b[MAXN],c[MAXN][MAXN];

int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n+1;i++)
        for (int j=1;j<=n;j++)
            scanf("%lf",&a[i][j]);
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        {
            c[i][j]=2*(a[i][j]-a[i+1][j]);
            b[i]+=a[i][j]*a[i][j]-a[i+1][j]*a[i+1][j];
        }
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=n;j++)
        {
            if (fabs(c[i][j]>1e-8))
            {
                for (int k=1;k<=n;k++)
                    swap(c[i][k],c[j][k]);
                swap(b[i],b[j]);
            }
        }
        for (int j=1;j<=n;j++)
            if (i!=j)
            {
                double tmp=c[j][i]/c[i][i];
                for (int k=i;k<=n;k++)
                    c[j][k]-=c[i][k]*tmp;
                b[j]-=b[i]*tmp;
            }
    }
    for (int i=1;i<n;i++)
        printf("%.3lf ",b[i]/c[i][i]);
    printf("%.3lf",b[n]/c[n][n]);
    return 0;
}

 

posted @ 2018-08-24 12:35  QingCai_DCF  阅读(143)  评论(0编辑  收藏  举报