[JSOI2008] spherical space generator sphere - Gaussian elimination

description

Given \ (n + 1 \) a \ (\ n-) dimensional coordinates, solving \ (n-\) - dimensional ball center

Thinking

Review questions Gaussian elimination , from the expression (x ^ 2, y ^ 2 \) \ eliminate, linear equations to obtain

Sequentially select the first \ (I \) column elimination, for convenience, not disappeared and then back into the upper triangular matrix band, but take a complete elimination

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

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

int main() {
    cin >> 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 = i; j <= n; ++ j) {
            if (fabs(c[j][i]) > 1e-8) {
                for (int k = 1; k <= n; ++ k) swap(c[i][k], c[j][k]);
                swap(b[i], b[j]);
                break;
            }
        }
        for (int j = 1; j <= n; ++ j) {
            if (i == j) continue;
            double rate = c[j][i] / c[i][i];
            for (int k = i; k <= n; ++ k) c[j][k] -= c[i][k] * rate;
            b[j] -= b[i] * rate;
        }
    }
    for (int i = 1; i < n; ++ i) printf("%.3f ", b[i] / c[i][i]);
    printf("%.3f\n", b[n] / c[n][n]);
}

Guess you like

Origin www.cnblogs.com/alessandrochen/p/11257799.html