昨天写了一个玩玩,可以实现.
procedure TForm1.Button2Click(Sender: TObject);
var
dd: array of array of integer;
i, j, rows, cols,rowe,cole, len: integer;
str: string;
count: integer;
iflag: integer
//±êʶ·½Ïò
begin
rows := strtoint(trim(edit1.Text));
cols := strtoint(trim(edit2.Text));
rowe:=1;
cole:=0;
setlength(dd, rows, cols);
len := rows * cols;
{for i := 0 to rows - 1 do
begin
str := '';
for j := 0 to cols - 1 do
begin
dd[j] := i*cols+j;
str := str + inttostr(dd[j]) + ' ';
end;
memo1.Lines.Add(str);
end
}
memo1.Lines.Add('-----------------------');
count := 1;
i := 0;
j := 0;
iflag := 1;
//1表示向右,2表示向下,三表示向左,四表示向上
while count <= len do
begin
dd[j] := count;
case iflag of
1:
begin
if (j = cols - 1) then
begin
iflag := 2;
cols := cols - 1;
i := i + 1;
end
else
j := j + 1;
end;
2:
if (i = rows - 1) then
begin
iflag := 3;
rows := rows - 1;
j := j - 1;
end
else
i := i + 1;
3:
if (j=cole) then
begin
cole:=cole+1;
iflag:=4;
i:=i-1;
end
else
j:=j-1;
4:
if (i=rowe) then
begin
iflag:=1;
rowe:=rowe+1;
j:=j+1;
end
else
i:=i-1;
end;
count := count + 1;
end;
for i := 0 to strtoint(trim(edit1.Text))-1 do
begin
str := '';
for j := 0 to strtoint(trim(edit2.Text))-1 do
begin
str := str + inttostr(dd[j]) + ' ';
end;
memo1.Lines.Add(str);
end;
memo1.Lines.Add('-----------------------');
end;