fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. int main() {
  6. int n,m;
  7. std::cin >> n >> m;
  8. std::vector<int> v;
  9.  
  10. for (int i = 0; i != n * m; i++) {
  11. int x;
  12. std::cin >> x;
  13. v.push_back(x);
  14. }
  15. std::sort(v.begin(), v.end());
  16. for (int i = 0; i < n; i++)
  17. {
  18. for (int j = 0; j < m; j++)
  19. std::cout << v[j * n + i] << " ";
  20. std::cout << '\n';
  21. }
  22. }
Success #stdin #stdout 0s 4548KB
stdin
5 4 

2 4 1 3
9 8 7 6
20 19 18 16
30 29 124 12
59 21 0 3
stdout
0 4 12 21 
1 6 16 29 
2 7 18 30 
3 8 19 59 
3 9 20 124