bzoj 1031

根据球面上的点到球心的距离相等列方程

高斯消元求解

#include <bits/stdc++.h>

#define DB double
const DB eps = 1e-10;
const int N = 15;

DB A[N][N], M[N][N], Answer[N];
int n;

DB read() {DB a; scanf("%lf", &a); return a;}

void Make_Matrix(int x, int y) {
    for(int i = 1; i <= n; i ++)
        M[x][i] = A[x][i] - A[y][i],
        M[x][n + 1] += (A[x][i] * A[x][i] - A[y][i] * A[y][i]);
    M[x][n + 1] /= 2;
}

void Gauss() {
    for(int i = 1; i <= n; i ++) {
        int r = i;
        for(int j = i + 1; j <= n; j ++) if(abs(M[j][i]) > abs(M[r][i])) r = i;
        if(i != r) std:: swap(M[i], M[r]);
        DB now = M[i][i];
        for(int j = i; j <= n + 1; j ++) M[i][j] /= now;
        for(int j = i + 1; j <= n; j ++) {
            DB Now = M[j][i];
            for(int k = i; k <= n + 1; k ++) M[j][k] -= Now * M[i][k];
        }
    }
    Answer[n] = M[n][n + 1];
    for(int i = n - 1; i >= 1; i --) {
        Answer[i] = M[i][n + 1];
        for(int j = n; j > n - (n - i); j --)
            Answer[i] -= Answer[j] * M[i][j];
    }
}

int main() {
    std:: cin >> n;
    for(int i = 1; i <= n + 1; i ++)
        for(int j = 1; j <= n; j ++)
            A[i][j] = read();
    for(int i = 1; i <= n; i ++) Make_Matrix(i, i + 1);
    Gauss();
    for(int i = 1; i <= n; i ++) printf("%.3lf ", Answer[i]);
    return 0;
}

 

posted @ 2018-06-03 14:21  qmey  阅读(104)  评论(0编辑  收藏  举报