BZOJ1013:[JSOI2008]球形空间产生器sphere (高斯消元)

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1013


题目分析:设题面给出的第i个坐标为(ai,1,ai,2ai,n),设答案为(b1,b2bn)。经过一番推导,令:

pi=2j=1nai,jbjj=1nai,j2

则所有pi的值相等。设其值为未知数k,就变成一个n+1个未知数的线性方程组,直接高斯消元即可。


CODE:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
using namespace std;

const int maxn=15;
const double eps=1e-9;

double a[maxn][maxn];
int n;

double Abs(double x)
{
    if (x>0.0) return x;
    return -x;
}

int Gauss()
{
    int R,r=1,c=1;
    while ( r<=n && c<=n )
    {
        R=r;
        for (int i=r+1; i<=n; i++)
            if ( Abs(a[i][c])>Abs(a[R][c]) ) R=i;

        if ( Abs(a[R][c])<eps ) r--;
        else
        {
            for (int i=c; i<=n+1; i++) swap(a[R][i],a[r][i]);
            for (int i=r+1; i<=n; i++)
                if ( Abs(a[i][c])>=eps )
                {
                    for (int j=c+1; j<=n+1; j++)
                        a[i][j]=a[i][j]/a[i][c]-a[r][j]/a[r][c];
                    a[i][c]=0.0;
                }
        }

        r++;
        c++;
    }

    for (int i=r; i<=n; i++)
        if ( Abs(a[i][n+1])>=eps ) return -1;
    if (r<=n) return n-r+1;

    for (int i=n; i>=1; i--)
    {
        for (int j=i+1; j<=n; j++) a[i][n+1]-=(a[i][j]*a[j][n+1]);
        a[i][n+1]/=a[i][i];
    }
}

int main()
{
    freopen("1013.in","r",stdin);
    freopen("1013.out","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]);
            a[i][n+2]+=(a[i][j]*a[i][j]);
            a[i][j]*=2.0;
        }
        a[i][n+1]=1.0;
    }

    n++;
    Gauss();
    for (int i=1; i<n; i++) printf("%.3lf ",a[i][n+1]);
    printf("\n");

    return 0;
}
发布了160 篇原创文章 · 获赞 76 · 访问量 10万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览