You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.8 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
const double eps = 1e-8;
int n;
double a[N][N];
/*
a[i][n]0 <= i < n
0: 1: 2:
*/
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; //是0不处理
swap(a[t], a[r]); // 将绝对值最大的行换到最顶端
for (int i = n; i >= c; i--) a[r][i] /= a[r][c]; // 将当前行的首位变成1,倒着来
for (int i = r + 1; i < n; i++) // 用当前行将下面所有的列消成0
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 - 1; 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 + 1; j++) //带等式右侧的结果值
cin >> a[i][j];
int t = gauss();
if (t == 2) //无解
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; // 去掉输出 -0.00 的情况
printf("%.2lf\n", a[i][n]);
}
}
return 0;
}