mstoyanov7

04.Snake

Jun 13th, 2021 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4.  
  5. std::string repeatWord(std::string& word) {
  6.     std::string repeated;
  7.     for (int i = 0; i < 100; ++i) {
  8.         repeated.append(word);
  9.     }
  10.     return repeated;
  11. }
  12.  
  13. void makeSnake(std::list<std::list<char>>& snakeWords, std::string& word, int& rows, int& cols) {
  14.     int wordIndex = 0;
  15.  
  16.     for (int i = 0; i < rows; ++i) {
  17.         std::list<char> row;
  18.         for (int j = 0; j < cols; ++j) {
  19.             if (i % 2 == 0) {
  20.                 row.push_back(word[wordIndex]);
  21.             }
  22.             else {
  23.                 row.push_front(word[wordIndex]);
  24.             }
  25.             wordIndex++;
  26.         }
  27.         snakeWords.push_back(row);
  28.     }
  29. }
  30.  
  31. void printSnake(std::list<std::list<char>>& snakeWords) {
  32.     for (auto& row : snakeWords) {
  33.         for (auto& elem : row) {
  34.             std::cout << elem;
  35.         }
  36.         std::cout << std::endl;
  37.     }
  38. }
  39.  
  40. int main() {
  41.     int rows, cols;
  42.     std::string word;
  43.     std::cin >> rows >> cols >> word;
  44.  
  45.     std::list<std::list<char>> snakeWords;
  46.     word = repeatWord(word);
  47.     makeSnake(snakeWords, word, rows, cols);
  48.     printSnake(snakeWords);
  49. }
Add Comment
Please, Sign In to add comment