2012-12-25 163 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的意思是这个他/她的数组是行主要的。 –