P3389 【模板】高斯消元法 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
/*
* @Author: zhl
* @Date: 2020-11-14 09:22:58
*/
#include<bits/stdc++.h>
using namespace std;
double A[110][110];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n + 1; j++) {
scanf("%lf", &A[i][j]);
}
}
for (int i = 1; i <= n; i++) {
int mx = i;
for (int j = i + 1; j <= n; j++) {
if (fabs(A[j][i]) > fabs(A[mx][i])) mx = j;
}
for (int j = 1; j <= n + 1; j++)swap(A[i][j], A[mx][j]);
if (fabs(A[i][i]) < 1e-8) {
puts("No Solution");
return 0;
}
for (int j = 1; j <= n; j++) {
if (i == j)continue;
double t = A[j][i] / A[i][i];
for (int k = i + 1; k <= n + 1; k++) {
A[j][k] -= A[i][k] * t;
}
}
}
for (int i = 1; i <= n; i++) {
printf("%.2f
", A[i][n + 1] / A[i][i]);
}
}