Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int maxn = 20;
  6. const double eps = 1e-5;
  7.  
  8. int n, b[maxn];
  9. valarray<double> a[maxn];
  10.  
  11. void gauss() {
  12.     for (int col = 0; col < n; ++col) {
  13.         int mx = col;
  14.         for (int row = col; row < n; ++row) {
  15.             if (a[row][col] > a[mx][col]) {
  16.                 mx = row;
  17.             }
  18.         }
  19.         swap(a[mx], a[col]);
  20.         for (int row = col + 1; row < n; ++row) {
  21.             double cf = a[row][col] / a[col][col];
  22.             a[row] -= cf * a[col];
  23.         }
  24.     }
  25. }
  26.  
  27. int main() {
  28.     ios_base::sync_with_stdio(false);
  29.     cin.tie(nullptr), cout.tie(nullptr);
  30.     cout << setprecision(10) << fixed;
  31.     cin >> n;
  32.     for (int i = 0; i < n; ++i) {
  33.         a[i].resize(n + 1);
  34.         for (int j = 0, x; j <= n; ++j) {
  35.             cin >> a[i][j];
  36.         }
  37.     }
  38.     gauss();
  39.     for (int row = 0; row < n; ++row) {
  40.         bool check_eq = true;
  41.         for (int col = row; col < n; ++col) {
  42.             if (abs(a[row][col]) > eps) {
  43.                 check_eq = false;
  44.             }
  45.         }
  46.         if (check_eq) {
  47.             cout << (abs(a[row][n]) < eps ? "infinity" : "impossible") << endl;
  48.         }
  49.     }
  50.     cout << "single\n";
  51.     for (int row = n - 1; row >= 0; --row) {
  52.         for (int col = n - 1; col >= row + 1; --col) {
  53.             a[row][n] -= a[row][col] * a[col][n];
  54.         }
  55.         a[row][n] /= a[row][row];
  56.     }
  57.     for (int i = 0; i < n; ++i) {
  58.         cout << a[i][n] << ' ';
  59.     }
  60.     cout << endl;
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement