fork download
  1. #include <iostream>
  2. #include <fstream>
  3. void m_qsort(int** m, int* cs, int M, int l, int r);
  4. template<class Cmp>
  5. bool matrix_sort(int** m, int N, int M, Cmp cmp);
  6. int** matrix_input(std::istream& _in, int& N, int& M);
  7. void matrix_output(std::ostream& _out, int** m, int N, int M);
  8. void matrix_free(int** m, int N);
  9.  
  10. struct icmp {
  11. int n;
  12. icmp(int _n):n(_n){}
  13.  
  14. bool operator () (int x) const {
  15. return ((x % n) == 0);
  16. }
  17. };
  18.  
  19. int main(void){
  20. int** m, N, M;
  21. /* ввод из файла
  22. std::ifstream fp("matrix.txt");
  23. m = matrix_input(fp, N, M);
  24. fp.close();
  25. if(m == NULL)
  26. return 1;
  27. matrix_output(std::cout, m, N, M);
  28. matrix_sort(m, N, M, icmp(3));
  29. matrix_output(std::cout, m, N, M);
  30. matrix_free(m, N);
  31. */
  32. m = matrix_input(std::cin, N, M);
  33. if(m == NULL)
  34. return 1;
  35. matrix_output(std::cout, m, N, M);
  36.  
  37. //сортируем строки матрицы по-числу кратному 3-ём
  38. matrix_sort(m, N, M, icmp(3));
  39. matrix_output(std::cout, m, N, M);
  40. matrix_free(m, N);
  41. return 0;
  42. }
  43.  
  44. //сортировка строк матрицы
  45. template<class Cmp>
  46. bool matrix_sort(int** m, int N, int M, Cmp cmp){
  47. int* cs = new (std::nothrow) int[N];
  48. if(cs == NULL)
  49. return false;
  50.  
  51. for(int i = 0; i < N; ++i){
  52. cs[i] = 0;
  53. for(int j = 0; j < M; ++j){
  54. if(cmp(m[i][j]))
  55. ++cs[i];
  56. }
  57. }
  58. m_qsort(m, cs, M, 0, N - 1);
  59. delete[] cs;
  60. return true;
  61. }
  62.  
  63. //ввод матрицы из входного потока
  64. int** matrix_input(std::istream& _in, int& N, int& M){
  65. int** m, i, j;
  66. if(!(_in >> N >> M) || (N <= 1) || (M <= 1))
  67. return NULL;
  68.  
  69. m = new (std::nothrow) int*[N];
  70. if(m == NULL)
  71. return NULL;
  72.  
  73. for(i = 0; i < N; ++i){
  74. m[i] = new (std::nothrow) int[M];
  75. if(m[i] == NULL){
  76. --i;
  77. goto err;
  78. }
  79.  
  80. j = 0;
  81. while((j < M) && (_in >> m[i][j]) && !_in.fail())
  82. ++j;
  83.  
  84. if(j != M)
  85. goto err;
  86. }
  87. return m;
  88. err:
  89. for(j = i; j >= 0; --j)
  90. delete[] m[j];
  91. delete[] m;
  92. return NULL;
  93. }
  94.  
  95. //вывод матрицы в выходной поток
  96. void matrix_output(std::ostream& _out, int** m, int N, int M){
  97. for(int i = 0; i < N; ++i){
  98. for(int j = 0; j < M; ++j)
  99. _out << m[i][j] << ' ';
  100. _out << std::endl;
  101. }
  102. _out << std::endl;
  103. }
  104.  
  105. //удаление матрицы
  106. void matrix_free(int** m, int N){
  107. for(int i = 0; i < N; ++i)
  108. delete[] m[i];
  109. delete[] m;
  110. }
  111.  
  112. //базовая быстрая сортировка
  113. void m_qsort(int** m, int* cs, int M, int l, int r){
  114. int p, i = l, j = r;
  115. if(l >= r)
  116. return;
  117.  
  118. p = cs[l + (r - l) / 2];
  119. while(i <= j){
  120. while(cs[i] < p)
  121. ++i;
  122. while(cs[j] > p)
  123. --j;
  124.  
  125. if(i <= j){
  126. for(int v = 0; v < M; ++v)
  127. std::swap(m[i][v], m[j][v]);
  128. std::swap(cs[i], cs[j]);
  129. ++i;
  130. --j;
  131. }
  132. }
  133.  
  134. if(j > l)
  135. m_qsort(m, cs, M, l, j);
  136. if(r > i)
  137. m_qsort(m, cs, M, i, r);
  138. }
Success #stdin #stdout 0s 3420KB
stdin
8 10
9 9 9 1 1 1 1 1 1 1
9 9 9 9 9 9 9 1 1 1
9 9 1 1 1 1 1 1 1 1
9 9 9 9 9 9 9 9 9 9
9 1 1 1 1 1 1 1 1 1
9 9 9 9 9 9 9 9 1 1
9 9 1 1 1 1 1 1 1 1
9 9 9 9 9 9 1 1 1 1
stdout
9 9 9 1 1 1 1 1 1 1 
9 9 9 9 9 9 9 1 1 1 
9 9 1 1 1 1 1 1 1 1 
9 9 9 9 9 9 9 9 9 9 
9 1 1 1 1 1 1 1 1 1 
9 9 9 9 9 9 9 9 1 1 
9 9 1 1 1 1 1 1 1 1 
9 9 9 9 9 9 1 1 1 1 

9 1 1 1 1 1 1 1 1 1 
9 9 1 1 1 1 1 1 1 1 
9 9 1 1 1 1 1 1 1 1 
9 9 9 1 1 1 1 1 1 1 
9 9 9 9 9 9 1 1 1 1 
9 9 9 9 9 9 9 1 1 1 
9 9 9 9 9 9 9 9 1 1 
9 9 9 9 9 9 9 9 9 9