以下方法基本可行,但还是有些问题,a里每列最大的数值是表示的是1,以此顺序排到10,10后的数序就填0了.而您现在是最大的不是1,请教一下在哪改一下呢?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
maxa=16;maxb=15;//定义常量
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Button3: TButton;
Button4: TButton;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
arrayA : array[0..maxa,0..maxb] of integer;
arrayb : array[0..maxa,0..maxb] of integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function maopao(var a: array of integer): tStringlist;//冒泡排序
var i,j,temp:integer;
list: tStringlist;
begin
for i := high(a) downto low(a) do
begin
for j := low(a) to i do
begin
if a[j]>a[j+1] then
begin
temp := a[j];
a[j] := a[j+1];
a[j+1] := temp;
end;
end;
end;
list := tstringlist.Create;
for i := low(a) to high(a) do
list.Add(inttostr(a));
result := list;
end;
function findvalue(a:array of integer;value:integer):integer;//对排序的数组查找序号
var
i : integer;
begin
for I := low(a) to high(a) do
if a=value then Result := i;
end;
procedure TForm1.Button1Click(Sender: TObject);//初始化数组a,随即产生10以内的数据
var
i ,j : integer;
begin
Randomize;
for i := 0 to maxa do
for j:= 0 to maxb do
begin
arrayA[i,j] := Random(10);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);//将数组a的内容显示在MEMO1中
var
i ,j : integer;
s : string;
begin
for i := 0 to maxa do
begin
s := '';
for j:= 0 to maxb do
s := s + inttostr(arraya[i,j])+#9;
Memo1.Lines.Add(s);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);//获得结果
var
a : array[0..maxa] of integer;
i,j,n : integer;
s : string;
begin
for j := 0 to maxb do
begin
for i := 0 to maxa do
a := arraya[i,j];
maopao(a);
for n := 0 to maxa do
begin
arrayb[n,j] := findvalue(a,arraya[n,j]) ;
end;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);//在memo2中显示结果
var
i,j : integer;
s : string;
begin
for i := 0 to maxa do
begin
s := '';
for j:= 0 to maxb do
s := s + inttostr(arrayb[i,j])+#9;
Memo2.Lines.Add(s);
end;
end;
end.
运行结果如下
6 0 3 2 6 0 2
7 7 9 7 4 7 1
4 3 2 8 9 2 1
5 3 3 3 9 5 3
7 2 1 1 3 2 4
8 3 1 5 0 4 0
4 9 4 7 4 4 5
4 1 8 3 9 1 3
5 1 4 7 9 4 6
6 1 8 9 3 2 5
7 4 6 7 3 3 8
3 4 3 6 8 5 6
6 2 4 1 5 6 3
8 7 3 1 4 7 4
6 6 0 0 6 6 6
4 5 6 5 6 1 6
0 9 2 5 0 9 6
1 5 1 0 8 8 6
B
4 0 4 3 2 2 0
5 4 3 3 1 3 1
4 3 0 1 4 2 5
2 2 5 5 4 0 5
0 5 2 5 0 5 5
1 2 1 1 5 4 5