Advertisement
Spocoman

07. Fill the Matrix

Jan 28th, 2024
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     string line;
  9.     getline(cin, line);
  10.  
  11.     int n = stoi(line.substr(0, line.find(',')));
  12.     string method = line.substr(line.size() - 1);
  13.  
  14.     vector<vector<int>> matrix;
  15.  
  16.     for (size_t i = 0; i < n; i++) {
  17.         matrix.push_back(vector<int>(n));
  18.     }
  19.  
  20.     int counter = 1;
  21.  
  22.     for (int c = 0; c < n; c++) {
  23.         if (c % 2 == 1 && method == "B") {
  24.             for (int r = n - 1; r >= 0; r--) {
  25.                 matrix[r][c] = counter++;
  26.             }
  27.         }
  28.         else {
  29.             for (int r = 0; r < n; r++) {
  30.                 matrix[r][c] = counter++;
  31.             }
  32.         }
  33.     }
  34.  
  35.  
  36.     for (int r = 0; r < n; r++) {
  37.         for (int c = 0; c < n; c++) {
  38.             cout << matrix[r][c] << ' ';
  39.         }
  40.         cout << endl;
  41.     }
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement