Advertisement
Guest User

gauss

a guest
Feb 25th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <fstream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. long double EPS = 0.0000000000001;
  9. ifstream cin("linear.in");
  10. ofstream cout("linear.out");
  11.  
  12. int main() {
  13.     cin.sync_with_stdio(false);
  14.     cout.sync_with_stdio(false);
  15.     int n;
  16.     cin >> n;
  17.     vector< vector<long double> > vv(n, vector<long double>(n + 1));
  18.     vector<int> where(n, -1);
  19.     vector<long double> ans(n, 0.0);
  20.     for (int i = 0; i < n; i++)
  21.         for (int j = 0; j < n + 1; j++)
  22.             cin >> vv[i][j];
  23.     int row = 0;
  24.     for (int col = 0; col < n && row < n; col++) {
  25.         int cur = row;
  26.         for (int i = cur; i < n; i++)
  27.             if (abs(vv[i][col]) > abs(vv[cur][col]))
  28.                 cur = i;
  29.         if (abs(vv[cur][col]) < EPS)
  30.             continue;
  31.         for (int i = col; i < n + 1; i++)
  32.             swap(vv[cur][i], vv[row][i]);
  33.         long double d = vv[row][col];
  34.         for (int i = col; i < n + 1; i++)
  35.             vv[row][i] /= d;
  36.         where[col] = row;
  37.         for (int i = row + 1; i < n; i++) {
  38.             long double c = vv[i][col];
  39.             for (int j = col; j < n + 1; j++)
  40.                 vv[i][j] -= vv[row][j] * c;
  41.         }
  42.         row++;
  43.     }
  44.     for(int i = row; i < n; i++)
  45.         if (abs(vv[i][n]) > EPS) {
  46.             cout << "impossible";
  47.             return 0;
  48.         }
  49.     for (int i = 0; i < n; i++)
  50.         if (where[i] == -1) {
  51.             cout << "infinity";
  52.             return 0;
  53.         }
  54.     for (int i = n - 1; i > -1; i--) {
  55.         long double c = vv[i][n];
  56.         for (int j = n - 1; j > i; j--)
  57.             c -= ans[j] * vv[i][j];
  58.         ans[i] = c / vv[i][i];
  59.     }
  60.     cout << "single" << endl;
  61.     for (int i = 0; i < n; i++)
  62.         cout << fixed << setprecision(3) << ans[i] << ' ';
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement