Advertisement
LegoSosiska

GAuss

May 18th, 2022
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. void cout(std::vector<std::vector<double>>& matrix) {
  6.     for (int i = 0; i < matrix.size(); ++i) {
  7.         for (int j = 0; j < matrix[i].size(); ++j) {
  8.             std::cout << matrix[i][j] << ' ';
  9.         }
  10.         std::cout << "\n";
  11.     }
  12.     std::cout << "\n";
  13. }
  14.  
  15. void dive(std::vector<std::vector<double>>& matrix, int i, int j) {
  16.     if (i >= matrix.size()) return;
  17.     if (matrix[i].size() <= j) return;
  18.     if (matrix[i][j] == 0) {
  19.         int i1 = i;
  20.         while (i1 < matrix.size() && matrix[i1][j] == 0) {
  21.             ++i1;
  22.         }
  23.         if (i1 >= matrix.size()) {
  24.             dive(matrix, i, j + 1);
  25.             return;
  26.         }
  27.         if (matrix[i1][j] == 0) {
  28.             dive(matrix, i, j + 1);
  29.             return;
  30.         }
  31.         std::swap(matrix[i], matrix[i1]);
  32.     }
  33.     for (int i1 = i + 1; i1 < matrix.size(); ++i1) {
  34.         double k = matrix[i1][j] / matrix[i][j];
  35.         for (int j1 = j; j1 < matrix[i1].size(); ++j1) {
  36.             matrix[i1][j1] -= matrix[i][j1] * k;
  37.             cout(matrix);
  38.         }
  39.     }
  40.     dive(matrix, i + 1, j + 1);
  41. }
  42.  
  43. int main() {
  44.     int n, m;
  45.     std::cin >> n >> m;
  46.     std::vector<std::vector<double>> matrix(n, std::vector<double>(m));
  47.     std::vector<std::string> ans;
  48.     for (int i = 0; i < n; ++i) {
  49.         for (int j = 0; j < m; ++j) {
  50.             std::cin >> matrix[i][j];
  51.         }
  52.     }
  53.     std::cout << "\n";
  54.     dive(matrix, 0, 0);
  55.     cout(matrix);
  56.     for (int i = matrix.size() - 1; i >= 0; --i) {
  57.         if (matrix[i][m - 1] == 0) continue;
  58.         if (matrix[i][m - 2] == 0) {
  59.             std::cout << "no roots";
  60.             return 0;
  61.         }
  62.         else {
  63.             char letters = 'a';
  64.             int ind = 0;
  65.             bool empt = false;
  66.             for (int j = m - 2; j > 0 && i > 0; --j, ++ind) {
  67.                 if (matrix[i][j - 1] != 0) {
  68.                     std::string s;
  69.                     s.push_back(letters);
  70.                     ans.push_back(s);
  71.                     letters = (letters + 1 - 'a') % 27 + 'a';
  72.                     empt = true;
  73.                     continue;
  74.                 }
  75.                 else {
  76.                     if (empt) {
  77.                         std::string s;
  78.                         s = std::to_string(matrix[i][j]);
  79.                         for (int j1 = j + 1; j1 < m; ++j1) {
  80.                             if (matrix[i][j1] == 0) continue;
  81.                             s += " - ";
  82.                             if (matrix[i][j1] != 1) {
  83.                                 std::string s1 = std::to_string(matrix[i][j1]);
  84.                                 s += '(';
  85.                                 s += s1;
  86.                                 s += ')';
  87.                                 s += '*';
  88.                             }
  89.                             s += '(';
  90.                             s += ans[ans.size() - j1];
  91.                             s += ')';
  92.                         }
  93.                         ans.push_back(s);
  94.                         std::cout << s << "\n";
  95.                         --i;
  96.                         continue;
  97.                     }
  98.                     else {
  99.                         std::string s;
  100.                         s = std::to_string(matrix[i][m - 1] / matrix[i][j]);
  101.                         ans.push_back(s);
  102.                         std::cout << s << "\n";
  103.                         --i;
  104.                         continue;
  105.                     }
  106.                     //сделать переменную отвечающую за прошлые корни и подконнектить к массиву анс, также флаг на присутствие до пустых членов
  107.                 }
  108.             }
  109.             if (empt) {
  110.                 std::string s;
  111.                 s = std::to_string(matrix[0][0]);
  112.                 for (int j1 = 1; j1 < m; ++j1) {
  113.                     if (matrix[0][j1] == 0) continue;
  114.                     s += " - ";
  115.                     if (matrix[0][j1] != 1) {
  116.                         std::string s1 = std::to_string(matrix[0][j1]);
  117.                         s += '(';
  118.                         s += s1;
  119.                         s += ')';
  120.                         s += '*';
  121.                     }
  122.                     s += '(';
  123.                     s += ans[ans.size() - j1];
  124.                     s += ')';
  125.                 }
  126.                 ans.push_back(s);
  127.                 std::cout << s << "\n";
  128.             }
  129.             else {
  130.                 std::string s;
  131.                 s = std::to_string(matrix[0][m - 1] / matrix[0][0]);
  132.                 ans.push_back(s);
  133.                 std::cout << s << "\n";
  134.             }
  135.             break;
  136.         }
  137.     }
  138.     for (int i = 0; i < ans.size(); ++i) {
  139.         std::cout << "x" << i + 1 << ": " << ans[ans.size() - 1 - i] << "\n";
  140.     }
  141. }
  142.  
  143. /*
  144. 4 4
  145. 1 1 -3 -1
  146. 2 1 -2 1
  147. 1 1 1 3
  148. 1 2 -3 1
  149.  
  150. 4 4
  151. 2 1 1 2
  152. 1 3 1 5
  153. 1 1 5 -7
  154. 2 3 -3 14
  155.  
  156.  
  157.  
  158. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement