Показать сообщение отдельно
  #4  
Старый 10.07.2012, 13:38
Pyro Pyro вне форума
Так проходящий
 
Регистрация: 18.07.2011
Сообщения: 805
Версия Delphi: 7Lite
Репутация: 6063
По умолчанию

алгоритм требует допиливания, думаю разберётесь
Код:
type
  TValue = integer;
  TRow = array of TValue;
  Tmatrix = array of TRow;

function row(values: array of TValue): TRow;
var i: integer;
begin
  SetLength(result, 0);

  for i := 0 to high(values) do
  begin
    SetLength(result, length(result)+1);
    result[length(result)-1] := values[i];
  end;
end;

function matrix(rows: array of tRow): Tmatrix;
var i: integer;
begin
  SetLength(result, 0);

  for i := 0 to high(rows) do
  begin
    SetLength(result, length(result)+1);
    result[length(result)-1] := rows[i];
  end;
end;

function to_str(this: TValue): string; overload;
begin
  result := IntToStr(this);
end;

function to_str(this: TRow): string; overload;
const delimiter = ', ';
var i: integer;
begin
  result := '';

  for i := 0 to high(this) do
  begin
    if i <> 0 then result := result + delimiter;
    result := result + to_str(this[i]);
  end;
end;

function to_str(this: Tmatrix): string; overload;
const delimiter = #13#10;
var i: integer;
begin
  result := '';

  for i := 0 to high(this) do
  begin
    if i <> 0 then result := result + delimiter;
    result := result + to_str(this[i]);
  end;
end;

function index_of_max_abs(this: TRow): integer;
var i: integer; max: TValue;
begin
  result := -1;
  max := -MaxInt - 1;
  for i := 0 to high(this) do
    if abs(this[i]) > max then
    begin
      max := abs(this[i]);
      result := i;
    end;
end;

function swap_rows(this: tmatrix; i, j: integer): tmatrix;
var k: TRow;
begin
  k := this[i];
  this[i] := this[j];
  this[j] := k;

  result := this;
end;

function max_abs_diagonal(matrix: tmatrix): tmatrix;
var i: integer;
begin
  for i := 0 to high(matrix) do
    matrix := swap_rows(matrix, i, index_of_max_abs(matrix[i]));
    
  result := matrix;
end;

procedure TForm1.Button1Click(Sender: TObject);
var one, two: Tmatrix;
begin
  one := matrix([row([1, 2, 3]), row([3, 2, 1]), row([2, 3, 1])]);
  two := max_abs_diagonal(one);
  Memo1.Text := to_str(two);
end;
Ответить с цитированием