Помощь - Поиск - Пользователи - Календарь
Полная версия: перестановка максимального и минимального элемента
Форум «Всё о Паскале» > Современный Паскаль и другие языки > Делфи
Bryklin
В квадратной матрице в каждой строке переставить ее максимальный и минимальный элементы.
Я начинала делать с помощью StringGrid. Дошла до момента сортировки и всё, дальше не знаю.
unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, Math;

type
  TForm3 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    StringGrid1: TStringGrid;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;
  Const
  n1=50; m1=50;
   min=1000;
     Max=0;
var
  Form3: TForm3;
   n,m,i,j:integer;
implementation

{$R *.dfm}


procedure TForm3.Button1Click(Sender: TObject);
begin
Close;
end;
procedure TForm3.FormCreate(Sender: TObject);
begin
  n:=4;                                        //число строк в массиве
  m:=4;                                        //число столбцов в массиве
  Edit1.Text:='';
  Edit2.Text:='';
 StringGrid1.Cells[0,0]:='I/J';
    for i:=1 to n do StringGrid1.Cells[0,i]:='i='+IntToStr(i);
      for j:=1 to m do StringGrid1.Cells[j,0]:='j='+IntToStr(j);
end;
procedure TForm3.Button2Click(Sender: TObject);
var i,j:integer;
begin
  n:=StrToInt(Edit1.Text);                            //ввод переменной n
  m:=StrToInt(Edit2.Text);                            //ввод переменной m
StringGrid1.RowCount:=n+1;                    //задание числа строк и столбцов в массиве
StringGrid1.ColCount:=m+1;
for i:=1 to n do StringGrid1.Cells[0,i]:='i='+IntToStr(i);
for j:=1 to m do StringGrid1.Cells[j,0]:='j='+IntToStr(j);

randomize;
    for i:=1 to n do
    for j:=1 to m do
    stringgrid1.cells[i,j]:=inttostr(random (100)-50);
end;

procedure TForm3.Button3Click(Sender: TObject);


end;

end.
Федосеев Павел
А зачем сортировать?
Тут достаточно в каждой строке найти мин и макс элементы (их позиции) и обменять местами.

И ещё мне непонятен следующий момент - в задании говорится о квадратной матрице, а у тебя в коде n*m. Но это сама разберёшься.

Покажу без StringGrid, чтобы был понятнее сам алгоритм:

пусть
  a: array[1..n, 1..n] of integer;    <--- эквивалент StringGrid

тогда

  for i:=1 to n do  {цикл по строкам}
  begin
    {в каждой строке ищем мин и макс элемент, а точнее их индексы}
    Jmin:=1;
    Jmax:=1;
    for j:=2 to n do
    begin
      if a[i, j] > a[i, Jmax] then
        Jmax:=j;
      if a[i, j] < a[i, Jmin] then
        Jmin:=j;
    end;
    {теперь обмениваем}
    tmp:=a[i, Jmin];
    a[i, Jmin]:=a[i, Jmax];
    a[i, Jmax]:=tmp;
  end;
nishaknapp
Why not settling on games that is fun and at the same time your earning. Well it'll make suspense because the game is well but dude just try it and it gave me hope while pandemic is real rn. How Online Casinos Came Into Being
Это текстовая версия — только основной контент. Для просмотра полной версии этой страницы, пожалуйста, нажмите сюда.