a1,a2,a3...a10:integer;
怎么比较这10个变量的大小并自动排序?
比如 a1=30;
a2=33
a3=503
.
.
.
a9=21
a10=5;
按赋值的大小自动排序
怎么比较这10个变量的大小并自动排序?
比如 a1=30;
a2=33
a3=503
.
.
.
a9=21
a10=5;
按赋值的大小自动排序
解决方案 »
- 连接sql server时,provider使用msdatashape有效率还是使用默认的sqlclient有效率?
- 求一个图片封装到DLL里面的例程
- 有沒有高手有辦法將delphi7的控件安裝到delphi2005中?能解決問題都給分。
- 关于字符串截取的问题
- 谁有TDBGRIDEh的控件包:ehlib Delphi7的版本啊?
- Socket错误:‘Asynchronous socket Error 10061’
- 用ADO打开Excel表之后,怎样知道Excel表里面有多少个工作表及其名称?急,不够分可再加.
- 后台备份SQL SERVER7数据库,如何终止?急!!!
- 请问怎样通过Dbgrid修改多条记录?
- HELP!!怎样在线程里添加NMUDP控件的onDataRecieved事件?
- 求教最优化算法,应该是背包算法的实现
- 乱码文件里查找指定字符串的大难题。求大神~
procedure TForm1.Button4Click(Sender: TObject);
var
aa: Array [0 .. 3] Of integer;
i: integer;
begin
aa[0] := 30;
aa[1] := 33;
aa[2] := 503;
aa[3] := 21;
Sort(aa);
for i := 0 to high(aa) do
Memo1.Lines.Add(inttostr(aa[i]));
end;procedure TForm1.Sort(var x: array of integer);
var
i, j, l: integer;
begin
for i := 0 to high(x) do
begin
for j := 0 to high(x) - 1 do
begin
if x[j] > x[j + 1] then
begin
l := x[j];
x[j] := x[j + 1];
x[j + 1] := l;
end;
end;
end;
end;结果:
21
30
33
503
aa[0] := 30=a0
aa[1] := 33=a1
aa[2] := 503=a2
aa[3] := 21=a3
我是这样写的
aa[1]:=a[1]+1=a1
这个等式没有办法直接赋值啊
能详细请教下吗 谢谢 麻烦了
是这样写的
aa[1]:=aa[1]+1=a1
这个等式没有办法直接赋值啊
能详细请教下吗 谢谢 麻烦了
非要用独立变量,也可以:uses System.Generics.Collections;procedure Sort(const A: array of PInteger);
var
N: array of integer;
i: integer;
begin
SetLength(N, Length(A));
for i := Low(A) to High(A) do
N[i] := A[i]^;
TArray.Sort<integer>(N);
for i := Low(A) to High(A) do
A[i]^ := N[i];
end;// 使用:
var
a1, a2, a3, a4, a5: integer;
begin
a1 := 30;
a2 := 33;
a3 := 503;
a4 := 21;
a5 := 5;
Sort([@a1, @a2, @a3, @a4, @a5]);
writeln(a1, ' ', a2, ' ', a3, ' ', a4, ' ', a5);
end;
非要用独立变量,也可以:uses System.Generics.Collections;procedure Sort(const A: array of PInteger);
var
N: array of integer;
i: integer;
begin
SetLength(N, Length(A));
for i := Low(A) to High(A) do
N[i] := A[i]^;
TArray.Sort<integer>(N);
for i := Low(A) to High(A) do
A[i]^ := N[i];
end;// 使用:
var
a1, a2, a3, a4, a5: integer;
begin
a1 := 30;
a2 := 33;
a3 := 503;
a4 := 21;
a5 := 5;
Sort([@a1, @a2, @a3, @a4, @a5]);
writeln(a1, ' ', a2, ' ', a3, ' ', a4, ' ', a5);
end;
我用的D7 用不了uses System.Generics.Collections; 这个。要高版本的才有好像
三楼代码已经实现了排序 但是排序出来是变量的值
变量=赋值 这个等式不行 aa[1]:=aa[1]+1=a1 能麻烦指教下应该怎么把值的排序替换成变量排序吗 麻烦了
var
N: array of integer;
i: integer;
procedure __Sort(var x: array of integer);
var
i, j, l: integer;
begin
for i := 0 to high(x) do
begin
for j := 0 to high(x) - 1 do
begin
if x[j] > x[j + 1] then
begin
l := x[j];
x[j] := x[j + 1];
x[j + 1] := l;
end;
end;
end;
end;
begin
SetLength(N, Length(A));
for i := Low(A) to High(A) do
N[i] := A[i]^;
//TArray.Sort<integer>(N);
__Sort(N);
for i := Low(A) to High(A) do
A[i]^ := N[i];
end;
for j := 0 to high(a) do
Memo1.Lines.Add(inttostr(a[j])); 过程可以执行 这个执行不了
的Sort
不要用__Sort,后者是内部嵌套过程
一样的 是变量的定义有问题吗 ?
a: Array [0.. 9] of integer; 是这样定义的
procedure TForm12.Button1Click(Sender: TObject);
var
aa: Array [0 .. 3] Of integer;
i: integer;
a0,a1,a2,a3:Integer;
begin
aa[0] := 30;
a0:=30;
aa[1] := 33;
a1:=33;
aa[2] := 503;
a2:=503;
aa[3] := 21;
a3:=21; Sort(aa);
for i := 0 to high(aa) do
begin
if aa[i] = a0 then
Memo1.Lines.Add('a0='+inttostr(aa[i]));
if aa[i] = a1 then
Memo1.Lines.Add('a1='+inttostr(aa[i]));
if aa[i] = a2 then
Memo1.Lines.Add('a2='+inttostr(aa[i]));
if aa[i] = a3 then
Memo1.Lines.Add('a3='+inttostr(aa[i]));
end;
end;procedure TForm12.Sort(var x: array of integer);
var
i, j, l: integer;
begin
for i := 0 to high(x) do
begin
for j := 0 to high(x) - 1 do
begin
if x[j] > x[j + 1] then
begin
l := x[j];
x[j] := x[j + 1];
x[j + 1] := l;
end;
end;
end;
end;结果如下:
a3=21
a0=30
a1=33
a2=503
a1=18
a7=18
a1=18
a7=18
a9=20
a4=23
a2=24
a8=24
a2=24
a8=24
a6=25
a3=26
a5=26
a3=26
a5=26
a0=29
i,j:integer;
begin
for i:=1 to 10-1 do
for j:=2 to 10 do
if A[j]<A[j] then
begin
k:=A[i];
A[i]:=B[j];
B[j]:=k;
end;
作一个排序即可。
end;
把Sort提到前面去。
procedure TForm16.Button2Click(Sender: TObject);
var
t1, t2, t0, new: string;
i: integer;
begin
t1 := '21,54,201,54,21,78,21,142';
t2 := 'AA,BB,CC,DD,EE,FF,GG,HH';
for i := 0 to 7 do
begin
t0 := GetManValue(t1, t2);
Memo1.Lines.add(t0);
new := '-' + (i + 1).ToString;
t1 := StringReplace(t1, copy(t0, 4, Length(t0) - 3), new, [rfIgnoreCase]);
end;
end;function TForm16.GetManValue(s1, s2: string): string;
var
ss1, ss2: TStringList;
ManIndex: integer;
i: integer;
begin
ss1 := TStringList.Create;
ss2 := TStringList.Create;
ss1.Delimiter := ',';
ss1.DelimitedText := s1;
ss2.Delimiter := ',';
ss2.DelimitedText := s2;
ManIndex := 0;
for i := 1 to ss1.Count - 1 do
begin
if StrToInt(ss1[i]) > StrToInt(ss1[ManIndex]) then
ManIndex := i;
end;
result := ss2[ManIndex] + '=' + ss1[ManIndex];
ss1.Free;
ss2.Free;
end;结果:
CC=201
HH=142
FF=78
BB=54
DD=54
AA=21
EE=21
GG=21
注意:变量必须定长。
t1 := StringReplace(t1, copy(t0, 4, Length(t0) - 3), '-1', []);
改成这样吧,'-1'取值是比你列出的数里面最小的数还小的随便一个数都行。
t1 := StringReplace(t1, copy(t0, pos('=', t0) + 1, Length(t0) - 3), '-1', []);
是啊,我最后输出的就可以做独立变量:aa,bb,cc......
map<int,string> key存变量的值,string存变量名....
var
a1,a2,a3,a4,a5:Integer;
aa:array [0..4] of Pint;
i,j:Integer;
procedure BubbleSort(b:array of Pint);
var
i,j,p:Integer;
t:Pint;
a:array of Integer;
begin
SetLength(a,Length(b));
for i:=Low(B) to High(B) do
a[i]:=b[i]^;
for i:=Low(A) to High(A)-1 do
for j:=i+1 to High(A) do
if a[i]>a[j] then
begin
p:=a[i];
a[i]:=a[j];
a[j]:=p;
end;
for i:=Low(B) to High(B) do
b[i]^:=a[i];end;
begin
a1:=20;
a2:=40;
a3:=10;
a4:=50;
a5:=30;
Memo1.Lines.Clear;
Memo1.Lines.Add(Format('Befroe Value:A1 = %d, A2 = %d, A3 = %d, A4 = %d, A5 = %d',[a1,a2,a3,a4,a5]));
BubbleSort([@a1,@a2,@a3,@a4,@a5]);
Memo1.Lines.Add(Format('After Value:A1 = %d, A2 = %d, A3 = %d, A4 = %d, A5 = %d',[a1,a2,a3,a4,a5]));
end;
procedure BubbleSort(b:array of Pint);
会产生复制数组
改成:
procedure BubbleSort(const b:array of Pint);
就可以直接排序了
int a1=a1-‘0’;