fork(1) download
  1. #include <stdint.h>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5.  
  6.  
  7. template <typename T>
  8. void MatrixTranspose(T &matr, typename T::size_type r, typename T::size_type c)
  9. {
  10. if ( r <= 1 || c <= 1 )
  11. return;
  12.  
  13. typedef typename T::size_type size_type;
  14. typedef typename T::value_type value_type;
  15.  
  16. size_type ind, ind_last;
  17. value_type buff;
  18.  
  19. ind_last = r * c - 2;
  20. while ( c > 1 )
  21. {
  22. for ( size_type i = r - 2; i != size_type(-1); --i )
  23. {
  24. ind = i * c + (c - 1);
  25. buff = matr.at(ind);
  26. while ( ind < ind_last )
  27. {
  28. matr.at(ind) = matr.at(ind + 1);
  29. ++ind;
  30. }
  31. matr.at(ind_last) = buff;
  32. --ind_last;
  33. }
  34. --ind_last;
  35. --c;
  36. }
  37.  
  38. return;
  39. }
  40.  
  41.  
  42.  
  43. template <typename T>
  44. void MatrixPrint(T &matr, typename T::size_type r, typename T::size_type c)
  45. {
  46. using namespace std;
  47.  
  48. typedef typename T::size_type size_type;
  49.  
  50. for ( size_type i = 0u; i != r; ++i )
  51. {
  52. for ( size_type j = 0u; j != c; ++j )
  53. cout << matr.at(i * c + j) << " ";
  54. cout << endl;
  55. }
  56. cout << endl;
  57.  
  58. return;
  59. }
  60.  
  61.  
  62.  
  63. int main()
  64. {
  65. using namespace std;
  66.  
  67. typedef vector<int32_t> matr_type;
  68. typedef matr_type::size_type size_type;
  69. typedef matr_type::value_type value_type;
  70.  
  71. size_type r, c;
  72. value_type buff;
  73. uint_fast32_t numberOfMatrix;
  74. matr_type matr;
  75.  
  76. cin >> numberOfMatrix;
  77. for ( uint_fast32_t counter = 0; counter != numberOfMatrix; ++counter )
  78. {
  79. r = c = 0;
  80. cin >> r >> c;
  81. for ( size_type ind = 0, end = r * c; ind != end; ++ind )
  82. {
  83. cin >> buff;
  84. matr.push_back(buff);
  85. }
  86.  
  87. //MatrixPrint(matr, r, c);
  88. MatrixTranspose(matr, r, c);
  89. MatrixPrint(matr, c, r);
  90. matr.clear();
  91. }
  92.  
  93.  
  94. return 0;
  95. }
Success #stdin #stdout 0s 4388KB
stdin
5

2 2

1 2
3 4


5 5

 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25


2 5

1 2 3 4 5
6 7 8 9 10


5 2

1 2
3 4
5 6
7 8
9 10


3 5

 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
stdout
1 3 
2 4 

1 6 11 16 21 
2 7 12 17 22 
3 8 13 18 23 
4 9 14 19 24 
5 10 15 20 25 

1 6 
2 7 
3 8 
4 9 
5 10 

1 3 5 7 9 
2 4 6 8 10 

1 6 11 
2 7 12 
3 8 13 
4 9 14 
5 10 15