2012-12-25 216 views
1

我有一個函數,檢查數組中是否有重複的數據;看起來如下所示。任務是使用該函數在矩陣中查找列,而不重複數據。問題是如何將矩陣的列傳遞給該函數。矩陣作爲陣列陣列

P.S.我使用帕斯卡/ Delphi的

type 
    myArray = array [1..10] of integer; 

function noRepeat(A: myArray; n: integer): Boolean; 
var 
    i: integer; 
begin 
    Result := true; 
    for i:=1 to n do 
    for j := i + 1 to n do 
     if (A[i] = A[j]) then 
     Result := true; 
end; 
+0

:對於i:= 0到n做對於j:= 0 m的[I,J]。 – 2012-12-25 21:02:09

+0

你的矩陣類型定義是怎樣的? – balazs

+0

通過const傳遞數組。不要複製它們。使用開放數組。不需要明確地傳遞長度,如果你的數組非常重要,那麼你需要將列挑出到一個臨時數組中,並通過它。 –

回答

4

在下面的例子:

matrix[column][row] 

是 'myMatrix的' 型是如何構造的。只需遍歷第一個數組並將其發送到您的測試函數。

此外,在測試功能中,請確保您將結果設置爲false以開始!

type 
    myArray = array [1..10] of integer; 
    myMatrix = array[1..10] of myArray; 

var 
    matrix: myMatrix; 

function noRepeat(const A: myArray): Boolean; 
var 
    i: integer; 
begin 
    //Result := true; //? 
    Result := false; 
    for i:=1 to high(A) do 
    for j := i + 1 to high(A) do 
     if (A[i] = A[j]) then 
     Result := true; 
end; 

procedure sendColumn; 
var 
    b, wasRepeat: boolean; 
    i: Integer; 
Begin 

    for i := low(matrix) to high(matrix) do 
    Begin 
    b := noRepeat(matrix[i]); 
    if b then 
     wasRepeat := true; 
    End; 

End; 

如果行很大,那麼你必須通知測試例程你想測試哪一列。

type 
    myArray = array [1..10] of integer; 
    myMatrix = array[1..10] of myArray; 

var 
    matrix: myMatrix; 

function noRepeat(const A: myMatrix; Col: Integer): Boolean; 
var 
    i, j: integer; 
begin 

    Result := false; 
    for i := low(A) to high(A) do  
    for j := i + low(A) to high(A) do 
     if (A[i][Col] = A[j][Col]) then 
     Result := true; 
end; 

procedure sendColumn; 
var 
    b, wasRepeat: boolean; 
    i: Integer; 
Begin 

    for i := 1 to 10 do 
    Begin 
    b := noRepeat(matrix, i); 
    if b then 
     wasRepeat := true; 
    End; 

End; 
在m×n矩陣,其中M是行大小並且N是列大小,這會給你colums
+0

thx很多,但是你的代碼發送到noRepeat函數的行,但不是矩陣的列 – user1448906

+1

無論你決定數組將被排序爲[列] [行]或[行] [列]完全取決於您。它本質上是相同的結構。 – AudioGL

+0

@user的意思是這個他/她的數組是行主要的。 –