bzoj 1013 球形空间产生器(高斯消元模板)

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1013
没啥好说的,放个模板,,防止以后忘了,,,
代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#define N 10
using namespace std;
int n;
double a[N + 5][N + 5],w[N + 5][N + 5];
void init(){
    scanf("%d",&n);
    for (int i = 1;i <= n + 1; ++i)
      for (int j = 1;j <= n; ++j)
        scanf("%lf",&w[i][j]);
    for (int i = 1;i <= n; ++i)
      for (int j = 1;j <= n; ++j){
        a[i][j] = -2.0*(w[i][j] - w[n + 1][j]);
        a[i][n + 1] += w[n + 1][j] * w[n + 1][j] - w[i][j] * w[i][j];}
}

inline void gauss(){
    for (int i = 1;i <= n; ++i){
        int j = i;
        while (!a[j][i] && j < n) ++j;
        swap(a[j],a[i]);
        double t = a[i][i];
        for (int j = 1;j <= n + 1; ++j) a[i][j] /= t;
        for (int j = i + 1;j <= n; ++j){
          double t = a[j][i];
          for (int k = 1;k <= n + 1; ++k)
            a[j][k] -= t * a[i][k];}
    }
    for (int i = n;i > 1; --i)
      for (int j = i - 1;j >= 1; --j)
        a[j][n + 1] -= a[j][i] * a[i][n + 1]; 
}

void print(){
    for (int i = 1;i < n; ++i) printf("%.3lf ",a[i][n + 1]);    
    printf("%.3lf",a[n][n + 1]);
}

int main(){
    init();
    gauss();
    print();
    return 0;
}

你可能感兴趣的:(bzoj 1013 球形空间产生器(高斯消元模板))