fork download
  1. program MoveMinFirstRow;
  2. uses crt;
  3.  
  4. type
  5. TMatrix = array of array of integer;
  6.  
  7. procedure PrintMatrix(var matrix: TMatrix; rows, cols: integer);
  8. var
  9. i, j: integer;
  10. begin
  11. for i := 0 to rows - 1 do
  12. begin
  13. for j := 0 to cols - 1 do
  14. write(matrix[i][j]:4);
  15. writeln;
  16. end;
  17. end;
  18.  
  19. procedure MoveMinFirstRow(var matrix: TMatrix; rows, cols: integer);
  20. var
  21. minRow, i, j: integer;
  22. temp: array of integer;
  23. begin
  24. // Находим строку с минимальным первым элементом
  25. minRow := 0;
  26. for i := 1 to rows - 1 do
  27. if matrix[i][0] < matrix[minRow][0] then
  28. minRow := i;
  29.  
  30. // Если минимальная строка уже первая, ничего не делаем
  31. if minRow = 0 then exit;
  32.  
  33. // Создаем временный массив для хранения минимальной строки
  34. SetLength(temp, cols);
  35. for j := 0 to cols - 1 do
  36. temp[j] := matrix[minRow][j];
  37.  
  38. // Сдвигаем строки вниз от начала до minRow
  39. for i := minRow downto 1 do
  40. for j := 0 to cols - 1 do
  41. matrix[i][j] := matrix[i-1][j];
  42.  
  43. // Копируем минимальную строку в первую позицию
  44. for j := 0 to cols - 1 do
  45. matrix[0][j] := temp[j];
  46. end;
  47.  
  48. var
  49. matrix: TMatrix;
  50. rows, cols, i, j: integer;
  51. begin
  52. clrscr;
  53.  
  54. // Пример матрицы из задания
  55. rows := 3;
  56. cols := 4;
  57.  
  58. SetLength(matrix, rows, cols);
  59.  
  60. // Заполняем матрицу
  61. matrix[0][0] := 2; matrix[0][1] := 4; matrix[0][2] := 1; matrix[0][3] := 5;
  62. matrix[1][0] := 3; matrix[1][1] := 5; matrix[1][2] := 2; matrix[1][3] := 7;
  63. matrix[2][0] := 1; matrix[2][1] := 2; matrix[2][2] := 1; matrix[2][3] := 4;
  64.  
  65. writeln('Исходная матрица:');
  66. PrintMatrix(matrix, rows, cols);
  67.  
  68. MoveMinFirstRow(matrix, rows, cols);
  69.  
  70. writeln;
  71. writeln('Матрица после перемещения:');
  72. PrintMatrix(matrix, rows, cols);
  73.  
  74. readkey;
  75. end.
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Исходная матрица:
   2   4   1   5
   3   5   2   7
   1   2   1   4

Матрица после перемещения:
   1   2   1   4
   2   4   1   5
   3   5   2   7