ChickyHas @ 2022-05-02 19:38:06
#include <bits/stdc++.h>
using namespace std;
const int N = 55;
const double eps = 1e-6;
int n;
double a[N][N];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n + 1; ++j) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; ++i) { //高斯-约旦消元
int r = i;
for (int j = i + 1; j <= n; ++j) {
if (abs(a[j][i]) > abs(a[r][i])) {
r = j;
}
}
if (r != i) {
swap(a[i], a[r]);
}
if (abs(a[i][i]) < eps) {
continue;
}
for (int j = 1; j <= n; ++j) {
if (j == i) {
continue;
}
double p = a[j][i] / a[i][i];
for (int k = i; k <= n + 1; ++k) {
a[j][k] -= a[i][k] * p;
}
}
double k = a[i][i];
for (int j = 1; j <= n + 1; ++j) {
a[i][j] /= k;
}
}
for (int i = 1; i <= n; ++i) { //判无穷解、无解
if (abs(a[i][i]) < eps) {
if (abs(a[i][n + 1]) > eps) {
cout << -1;
} else {
cout << 0;
}
return 0;
}
}
for (int i = 1; i <= n; ++i) {
cout << 'x' << i << '=';
printf("%.2lf\n", a[i][n + 1]);
}
return 0;
}
by ChickyHas @ 2023-05-07 10:45:38
都一年前的帖子了,居然没有人回复我