fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. void printMatrix(const vector<vector<int>>& matrix) {
  8. for (const vector<int>& row : matrix) {
  9. for (int element : row) {
  10. cout << element << " ";
  11. }
  12. cout << endl;
  13. }
  14. }
  15.  
  16. void sortAboveMainDiagonal(vector<vector<int>>& matrix) {
  17. int n = matrix.size();
  18.  
  19. for (int row = 1; row < n; ++row) {
  20.  
  21. vector<int> subdiagonal(row + 1);
  22. for (int col = 0; col <= row; ++col) {
  23. subdiagonal[col] = matrix[row - col][col];
  24. }
  25.  
  26. sort(subdiagonal.begin(), subdiagonal.end());
  27.  
  28. for (int col = 0; col <= row; ++col) {
  29. matrix[row - col][col] = subdiagonal[col];
  30. }
  31. }
  32. }
  33.  
  34. int main() {
  35.  
  36. vector<vector<int>> matrix = {
  37. {9, 4, 5},
  38. {3, 2, 7},
  39. {6, 8, 1},
  40. };
  41.  
  42. cout << "Исходный массив:" << endl;
  43. printMatrix(matrix);
  44.  
  45. sortAboveMainDiagonal(matrix);
  46.  
  47. cout << "\nОтсортированный массив:" << endl;
  48. printMatrix(matrix);
  49.  
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Исходный массив:
9 4 5 
3 2 7 
6 8 1 

Отсортированный массив:
9 4 6 
3 5 7 
2 8 1