#include <bits/stdc++.h> #define dbg(x) std::cerr << #x << "=" << x << "/n" using i64 = long long; const int N = 105; std::vector<double> f[N]; void output(int n) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n + 1; j++) { std::cerr << f[i][j] << " /n"[j == n + 1]; } } std::cerr << "/n"; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout << std::fixed << std::setprecision(2); std::cerr << std::fixed << std::setprecision(2); int n; std::cin >> n; for (int i = 1; i <= n; i++) { f[i].resize(n + 2); for (int j = 1; j <= n + 1; j++) { std::cin >> f[i][j]; } } //运用矩阵的左上到右下的对角线性质算答案 for (int i = 1; i <= n; i++) {//第i行第i列的数拿去减掉其他行的数 if (f[i][i] == 0) { int flag = 1; for (int j = i; j <= n; j++) { if (f[j][i] == 0) continue; std::swap(f[i], f[j]); //output(n); flag = 0; break; } if (flag) { std::cout << "No Solution/n"; return 0; } } for (int j = 1; j <= n; j++) { if (j == i) continue;//不是自己这行 if (f[j][i] == 0) continue;//如果自己是0就不用减了,加快速度 double coef = f[j][i] / f[i][i]; for (int k = 1; k <= n + 1; k++) { f[j][k] -= f[i][k] * coef; } //output(n); } } for (int i = 1; i <= n; i++) { if (f[i][i] == 0) { std::cout << "No Solution/n"; return 0; } } for (int i = 1; i <= n; i++) { if (f[i][n + 1] == 0) std::cout << 0.00 << "/n"; else std::cout << 1.0 * f[i][n + 1] / f[i][i] << "/n"; } return 0; }
原创文章,作者:端木书台,如若转载,请注明出处:https://blog.ytso.com/276422.html