Advertisement
Spocoman

01. Compare Matrices

Jan 27th, 2024 (edited)
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     int n, el;
  10.     cin >> n;
  11.     cin.ignore();
  12.  
  13.     string line;
  14.  
  15.     vector<vector<int>> matrix1;
  16.  
  17.     for (int i = 0; i < n; i++) {
  18.         vector<int> row;
  19.         getline(cin, line);
  20.         istringstream ss(line);
  21.  
  22.         while (ss >> el) {
  23.             row.push_back(el);
  24.         }
  25.  
  26.         matrix1.push_back(row);
  27.     }
  28.  
  29.     int n2, el2;
  30.     cin >> n2;
  31.     cin.ignore();
  32.  
  33.     string line2;
  34.  
  35.     vector<vector<int>> matrix2;
  36.  
  37.     for (int i = 0; i < n2; i++) {
  38.         vector<int> row;
  39.         getline(cin, line2);
  40.         istringstream ss(line2);
  41.  
  42.         while (ss >> el2) {
  43.             row.push_back(el2);
  44.         }
  45.  
  46.         matrix2.push_back(row);
  47.     }
  48.  
  49.     bool isequal = true;
  50.  
  51.     if (matrix1.size() != matrix2.size()) {
  52.         isequal = false;
  53.     }
  54.     else {
  55.         for (int i = 0; i < matrix1.size(); i++) {
  56.             if (matrix1[i].size() != matrix2[i].size()) {
  57.                 isequal = false;
  58.             }
  59.             else {
  60.                 for (int j = 0; j < matrix1[i].size(); j++) {
  61.                     if (matrix1[i][j] != matrix2[i][j]) {
  62.                         isequal = false;
  63.                         break;
  64.                     }
  65.                 }
  66.             }
  67.             if (!isequal) {
  68.                 break;
  69.             }
  70.         }
  71.     }
  72.  
  73.     cout << (isequal ? "equal" : "not equal") << endl;
  74.     return 0;
  75. }
  76.  
  77. Tarikat edition - don't do this at work:)
  78.  
  79. #include <iostream>
  80. #include <string>
  81. #include <vector>
  82.  
  83. using namespace std;
  84.  
  85. int main() {
  86.    int n, n2;
  87.    cin >> n;
  88.    cin.ignore();
  89.  
  90.    string line;
  91.  
  92.    vector<string> matrix1;
  93.  
  94.    for (int i = 0; i < n; i++) {
  95.        getline(cin, line);
  96.        matrix1.push_back(line);
  97.    }
  98.  
  99.    cin >> n2;
  100.    cin.ignore();
  101.  
  102.    if (n != n2) {
  103.        cout << "not ";
  104.    }
  105.    else {
  106.        for (int i = 0; i < matrix1.size(); i++) {
  107.            getline(cin, line);        
  108.            if (matrix1[i] != line) {
  109.                cout << "not ";
  110.                break;
  111.            }
  112.        }
  113.    }
  114.  
  115.    cout << "equal" << endl;
  116.    return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement