2013-03-20 1 views
-1

3 (4x4) 행렬 (다른 수, 동일한 레이아웃)을 출력하는 프로그램이 있습니다.가장 큰 요소 수 합계 (FreePacal)를 갖는 3 개의 행렬 중 어느 것입니까?

그리고 가장 큰 요소 수 합계를 다시 출력해야합니다.

예를 들어, 65는 가장 큰 요소 수 합계입니다.

1 2 3 4 10  1 2 3 4 10  1 1 1 1 4 
5 6 7 8 26  2 3 4 5 14  2 2 2 2 8 
9 1 2 3 15  3 4 5 6 18  3 3 3 3 12 
2 3 4 5 14  4 5 6 7 22  4 4 4 4 16 

     65     64     40 

3 랜덤 행렬을 생성하는 프로그램 :

uses 
    SysUtils; 

var 
    i: integer; 
    x: integer; 
    y: integer; 
    matrix: array[1..4, 1..4] of integer; 

begin 
    randomize; 

    for i := 1 to 3 do 
    begin 
    for x := 1 to 4 do 
     for y := 1 to 4 do 
     matrix[x, y] := random(101); 

    for x := 1 to 4 do 
    begin 
     for y := 1 to 4 do 
     write(IntToStr(matrix[x, y]), ' '); 
     writeln; 
    end; 
    writeln; 
    end; 
    readln; 
end. 

당신은 나를 도울 수 있습니까? 나는 매우 감사 할 것입니다.

답변

1

는 예를 들어이 방법이 될 수 :

program Project1; 

uses 
    SysUtils; 

// create a type for the matrix 
type 
    TMatrix = array[1..4, 1..4] of Integer; 

var 
    I: Integer; 
    X: Integer; 
    Y: Integer; 
    CurSum: Integer; 
    MaxIdx: Integer; 
    MaxSum: Integer; 
    Matrices: array[1..3] of TMatrix; 
begin 
    // initialize random seed 
    Randomize; 
    // initialize max. sum matrix index and max. matrix sum 
    MaxIdx := 0; 
    MaxSum := 0; 
    // iterate to create 3 matrices 
    for I := 1 to 3 do 
    begin 
    // initialize sum value of this matrix to 0 
    CurSum := 0; 
    // iterate to fill the matrices with random values 
    for X := 1 to 4 do 
     for Y := 1 to 4 do 
     begin 
     // to the matrix I assign a random value to the X, Y position 
     Matrices[I][X, Y] := Random(101); 
     // add this random value to the current matrix sum value 
     CurSum := CurSum + Matrices[I][X, Y]; 
     end; 
    // check if this matrix sum value is greater than the stored one 
    // and if so, then... 
    if CurSum > MaxSum then 
    begin 
     // store this matrix index 
     MaxIdx := I; 
     // and store this matrix sum as a max sum value 
     MaxSum := CurSum; 
    end; 
    // print out this matrix 
    for X := 1 to 4 do 
    begin 
     for Y := 1 to 4 do 
     Write(IntToStr(Matrices[I][X, Y]), ' '); 
     WriteLn; 
    end; 
    WriteLn; 
    end; 
    // print out the index of the matrix with max sum and its sum value 
    WriteLn; 
    WriteLn('The biggest matrix is the ' + IntToStr(MaxIdx) + '. one. The sum ' + 
    'of this matrix is ' + IntToStr(MaxSum) + '.'); 
    WriteLn; 
    // and print out that matrix with max sum value 
    for X := 1 to 4 do 
    begin 
    for Y := 1 to 4 do 
     Write(IntToStr(Matrices[MaxIdx][X, Y]), ' '); 
    WriteLn; 
    end; 

    ReadLn; 
end. 
+0

감사합니다, 당신은 최고입니다! –

+1

여러분을 환영합니다! – TLama

관련 문제