fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <chrono>
  4. #include <random>
  5. #include <vector>
  6. #include <iomanip>
  7.  
  8. template <class IntType>
  9. IntType random(IntType a, IntType b) {
  10. static std::minstd_rand generator(
  11. static_cast<unsigned>(
  12. std::chrono::system_clock::now().time_since_epoch().count()
  13. )
  14. );
  15. std::uniform_int_distribution<int> distribution(a, b);
  16. return distribution(generator);
  17. }
  18.  
  19. using Matrix = std::vector<std::vector<int>>;
  20. std::ostream& operator<< (std::ostream& out, Matrix const& matrix) {
  21. int offset = out.width();
  22. for(auto const& row: matrix) {
  23. out << std::setw(offset);
  24.  
  25. for(auto const& element: row) {
  26. out << ' ' << std::setw(4) << std::left << element;
  27. }
  28.  
  29. out << '\n';
  30. }
  31.  
  32. return out;
  33. }
  34. int main() {
  35. Matrix matrix(random(5u, 10u));
  36. std::generate(matrix.begin(), matrix.end(), [] {
  37. std::vector<int> row(random(5u, 10u));
  38. std::generate(row.begin(), row.end(), [] { return random(0, 100); });
  39. return row;
  40. });
  41.  
  42. std::cout << std::setw(5) << matrix << '\n';
  43.  
  44. int k = 4;
  45. for(size_t row = 1; row < matrix.size(); row += 2) {
  46. std::rotate(matrix[row].begin(), matrix[row].begin() + k, matrix[row].end());
  47. }
  48.  
  49. std::cout << "After rotate odd rows on " << k << " elements to left: \n"
  50. << std::setw(5) << matrix << std::endl;
  51.  
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 4220KB
stdin
Standard input is empty
stdout
     41   37   31   90   44   11   45   18   17   9   
     62   53   61   60   42   21   61   14   17   16  
     54   58   90   3    28  
     59   45   78   32   55   13  
     5    38   32   97   26   48   59   100  97  
     19   87   58   10   5   
     12   43   8    78   92   88   98   50   12   26  

After rotate odd rows on 4 elements to left: 
     41   37   31   90   44   11   45   18   17   9   
     42   21   61   14   17   16   62   53   61   60  
     54   58   90   3    28  
     55   13   59   45   78   32  
     5    38   32   97   26   48   59   100  97  
     5    19   87   58   10  
     12   43   8    78   92   88   98   50   12   26