gpt4 book ai didi

arrays - 二维数组的唯一编号分配

转载 作者:行者123 更新时间:2023-12-02 08:31:03 24 4
gpt4 key购买 nike

我试图将 R*C/2-1 唯一数字写入数组,但随机函数多次给出相同的值。应在随机位置将每个数字填充 2 次。

喜欢:

 ( 1 2 3 4 )
( 5 6 7 8 )
( 1 2 3 4 )
( 5 6 7 8 )

到目前为止我的代码:

...

R=4
C=4

...

 var
somearray : array [0 .. (C- 1), 0 .. (R- 1)] of integer;

...

    for Row := 0 to (R - 1) do
for Col := 0 to (C - 1) do
begin
somearray [Col, Row] := RandomRange(0, 9);
end;

...

编辑1:

第二个数组

...

var
maxnum: array [0 .. (C * R) div 2 - 1] of integer;


max := (C * R) div 2;
for i := 0 to max - 1 do
maxnum[i] := i;

...

maxnum 的数字为 0..7/1-8

最佳答案

我认为你有两种方法可以做到这一点:

随机位置

迭代这些数字,然后将每个数字放置在随机位置。如果您遇到冲突位置,那么您必须有一些逻辑来找到该值的下一个位置。

使用您的代码我们会得到:

// Initialize the array
for Row := 0 to (R - 1) do
for Col := 0 to (C - 1) do
somearray[Col, Row] := 0;
// Populate the random numbers
for randomCount := 1 to 2 do
for randomNumber := 1 to ((R * C) div 2) do
begin
randomPosition := Random(R * C);
while somearray[randomPosition div C, randomPosition mod C] <> 0 do
begin
Inc(randomPosition);
if randomPosition >= (R * C) then
randomPosition := 0;
end;
somearray[randomPosition div C, randomPosition mod C] := randomNumber;
end;

将其放在更通用的形式中:

type
T2DArray = array of array of Integer;

procedure RandomPopulate1(aTheArray: T2DArray);
var
col: Integer;
colCount: Integer;
randomCount: Integer;
randomNumber: Integer;
randomPosition: Integer;
row: Integer;
rowCount: Integer;
begin
// Initialize the array
colCount := Length(aTheArray);
if colCount = 0 then
Exit;
rowCount := Length(aTheArray[0]);
for col := 0 to colCount - 1 do
for row := 0 to rowCount - 1 do
aTheArray[col, row] := 0;
// Populate the random numbers
for randomCount := 1 to 2 do
for randomNumber := 1 to ((colCount * rowCount) div 2) do
begin
randomPosition := Random(colCount * rowCount);
while aTheArray[randomPosition div colCount, randomPosition mod colCount] <> 0 do
begin
Inc(randomPosition);
if randomPosition >= (colCount * rowCount) then
randomPosition := 0;
end;
aTheArray[randomPosition div colCount, randomPosition mod colCount] := randomNumber;
end;
end;

要使用这个:

var
somearray: T2DArray;
begin
Randomize;
SetLength(somearray, 4, 4);
RandomPopulate1(somearray);
end;

使用随机位置改变

您可以设置数组中的值,然后随机化位置。因此执行多次随机交换。这是更简单的选择。

// Initialize the array
for Row := 0 to (R - 1) do
for Col := 0 to (C - 1) do
somearray[Col, Row] := ((Col * C + Row) div 2) + 1;
// Now randomize the positions
for randomLoop := 0 to (R * C) - 1 do
begin
randomPosition := Random(R * C);
randomNumber := somearray[randomLoop div C, randomLoop mod C];
somearray[randomLoop div C, randomLoop mod C] := somearray[randomPosition div C, randomPosition mod C];
somearray[randomPosition div C, randomPosition mod C] := randomNumber;
end;

和以前一样,我们可以有一个更通用的版本:

procedure RandomPopulate2(aTheArray: T2DArray);
var
col: Integer;
colCount: Integer;
randomLoop: Integer;
randomNumber: Integer;
randomPosition: Integer;
row: Integer;
rowCount: Integer;
begin
// Initialize the array
colCount := Length(aTheArray);
if colCount = 0 then
Exit;
rowCount := Length(aTheArray[0]);
for col := 0 to colCount - 1 do
for row := 0 to rowCount - 1 do
aTheArray[col, row] := ((col * colCount + row) div 2) + 1;
// Now randomize the positions
for randomLoop := 0 to ((colCount * rowCount) div 2) do
begin
randomPosition := Random(colCount * rowCount);
randomNumber := aTheArray[randomLoop div colCount, randomLoop mod colCount];
aTheArray[randomLoop div colCount, randomLoop mod colCount] :=
aTheArray[randomPosition div colCount, randomPosition mod colCount];
aTheArray[randomPosition div colCount, randomPosition mod colCount] := randomNumber;
end;
end;

关于arrays - 二维数组的唯一编号分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33660697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com