1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 105; 4 typedef double Matrix[maxn][maxn]; 5 void gauss_elimination(Matrix &A, int n) { 6 // 消元过程 7 int i, j, k, r; 8 for (i = 0; i < n; ++i) { 9 // 选一行r并与第i行交换 10 r = i; 11 for (j = i+1; j < n; ++j) 12 if (fabs(A[j][i]) > fabs(A[r][i])) r = j; 13 if (r != i) for (j = 0; j <= n; ++j) swap(A[r][j], A[i][j]); 14 15 if (!A[i][i]) { 16 puts("No Solution"); 17 exit(0); 18 } 19 /* 20 // 与第i+1~n进行消元,低精度 21 for (k = i+1; k < n; ++k) { 22 double f = A[k][i] / A[i][i]; 23 for (j = i; j <= n; ++j) A[k][j] -= f * A[i][j]; 24 } 25 */ 26 for (j = n; j >= i; --j) 27 for (k = i+1; k < n; ++k) 28 A[k][j] -= A[k][i]/A[i][i] * A[i][j]; 29 } 30 // 回代过程 31 for (i = n-1; i >= 0; --i) { 32 for (j = i+1; j < n; ++j) 33 A[i][n] -= A[j][n] * A[i][j]; 34 A[i][n] /= A[i][i]; 35 } 36 } 37 int main() { 38 Matrix mat; 39 int n; scanf("%d",&n); 40 for (int i = 0; i < n; ++i) { 41 for (int j = 0; j < n+1; ++j) { 42 scanf("%lf",&mat[i][j]); 43 } 44 } 45 gauss_elimination(mat,n); 46 for (int i = 0; i < n; ++i) { 47 printf("%.2f\n",mat[i][n]); 48 } 49 return 0; 50 }