0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:11:58 +00:00

P3389 【模板】高斯消元法

https://www.luogu.com.cn/record/75484018
This commit is contained in:
Baoshuo Ren 2022-05-10 19:01:34 +08:00
parent e1387610b5
commit b79303d612
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

75
Luogu/P3389/P3389.cpp Normal file
View File

@ -0,0 +1,75 @@
#include <cmath>
#include <iomanip>
#include <iostream>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 105;
const double eps = 1e-6;
int n;
double a[N][N];
int gauss() {
int r, c;
for (r = c = 1; c <= n; c++) {
int t = r;
for (int i = r; i <= n; i++) {
if (std::abs(a[i][c]) > std::abs(a[t][c])) t = i;
}
if (std::abs(a[t][c]) < eps) continue;
std::swap(a[t], a[c]);
for (int i = n + 1; i >= c; i--) {
a[r][i] /= a[r][c];
}
for (int i = r + 1; i <= n; i++) {
if (std::abs(a[i][c]) > eps) {
for (int j = n + 1; j >= c; j--) {
a[i][j] -= a[i][c] * a[r][j];
}
}
}
r++;
}
if (r <= n) return -1;
for (int i = n; i; i--) {
for (int j = i + 1; j <= n; j++) {
a[i][n + 1] -= a[i][j] * a[j][n + 1];
}
}
return 0;
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n + 1; j++) {
cin >> a[i][j];
}
}
if (!gauss()) {
for (int i = 1; i <= n; i++) {
cout << std::fixed << std::setprecision(2) << a[i][n + 1] << endl;
}
} else {
cout << "No Solution" << endl;
}
return 0;
}