[JSOI2008 sphere]

[关键字]:高斯消元

[题目大意]:给出一个n维空间中球面上n+1个点,求出球心坐标。

//===========================================================================================

[分析]:看他给出的条件n+1个点,要求出n个值,感觉像解方程租。因为,每个点到球心的坐标相等。设圆心坐标为(x1,x2,x2......xn),每个点到圆心的距离为di,然后就可以列出n个式子d1=d2\d2=d3......dn=dn+1,然后化简一下利用高斯消元解出就行了。

[代码]:

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

#include<cmath>

using namespace std;



const int MAXN=100;

const double ZERO=1e-8;



int n;

double f[MAXN],a[MAXN][MAXN],b[MAXN][MAXN];



int main()

{

    freopen("in.txt","r",stdin);

    freopen("out.txt","w",stdout);

    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+1;++i)

    {

        for (int j=1;j<=n;++j)

            printf("%.3lf ",a[i][j]);

        printf("\n");

    }*/

    for (int i=1;i<=n+1;++i)

    {

        double temp=0;

        for (int j=1;j<=n;++j)

        {

            temp+=a[i][j]*a[i][j];

            a[i][j]=2*a[i][j];

        }

        a[i][n+1]=temp;

    }

    for (int i=1;i<=n;++i)

        for (int j=1;j<=n+1;++j)

            b[i][j]=a[i][j]-a[i+1][j];

    /*for (int i=1;i<=n+1;++i)

    {

        for (int j=1;j<=n+1;++j)

            printf("%.3lf ",a[i][j]);

        printf("\n");

    }*/

    for (int i=1;i<=n;++i)

    {

        int j=i;

        while (fabs(b[j][i])<ZERO) ++j;

        for (int x=1;x<=n+1;++x)

            swap(b[i][x],b[j][x]);

        for (int j=i+1;j<=n;++j)

            if (fabs(b[j][i])>ZERO)

            {

                                   double temp=b[j][i]/b[i][i];

                                   for (int k=i;k<=n+1;++k)

                                       b[j][k]-=temp*b[i][k];

            }

    }

    for (int i=n;i>=1;--i)

    {

        double temp=b[i][n+1];

        for (int j=i+1;j<=n;++j) temp-=f[j]*b[i][j];

        f[i]=temp/(b[i][i]);

    }

    for (int i=1;i<=n;++i)

        printf("%.3lf ",f[i]);

    fclose(stdin);

    fclose(stdout);

    return 0;

}

你可能感兴趣的:(2008)