Files
python/TangDou/AcWing/Gauss/P3389.cpp

64 lines
1.5 KiB
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
const double eps = 1e-8;
double a[N][N];
int n;
int gauss() {
int c, r;
for (c = 0, r = 0; c < n; c++) {
int t = r;
for (int i = r; i < n; i++)
if (abs(a[i][c]) > abs(a[t][c])) t = i;
if (abs(a[t][c]) < eps) continue;
if (r != t) swap(a[t], a[r]);
for (int i = n; i >= c; i--) a[r][i] /= a[r][c];
for (int i = r + 1; i < n; i++)
for (int j = n; j >= c; j--)
a[i][j] -= a[r][j] * a[i][c];
r++;
}
if (r < n) {
for (int i = r; i < n; i++)
if (abs(a[i][n]) > eps)
return 2;
return 1;
}
for (int i = n - 2; i >= 0; i--)
for (int j = i + 1; j < n; j++)
a[i][n] -= a[i][j] * a[j][n];
return 0;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j <= n; j++)
cin >> a[i][j];
int t = gauss();
//经验教训:
// 1、一定要复制题目中输出的字符串,我就是因为"No Solution" -> "No solution"挂了第一个点
// 2、Luogu的模板题中,没有强制区分无解和无穷多组解。
if (t == 2 || t == 1)
puts("No Solution");
// else if (t == 1)
// puts("Infinite group solutions");
else {
for (int i = 0; i < n; i++) {
if (abs(a[i][n]) < eps) a[i][n] = 0;
printf("%.2lf\n", a[i][n]);
}
}
return 0;
}