呵呵,终于明白LZ的问题了,这个程序应该可以满足你的要求了
//交换两个指针指向的整数
procedure jh(psi, pdi: PInteger);
var
tmp: Integer;
begin
tmp := psi^;
psi^ := pdi^;
pdi^ := tmp;
end;
function px(psi, pdi: PInteger;
n: Integer;
iCount: Integer): Boolean;
var
tmppdi: PInteger;
begin
Result := False;
if (psi^ mod 2) = 0 then
begin
if (pdi^ mod 2) = 1 then
jh(psi, pdi)
else
begin
if n < iCount - 2 then
begin
tmppdi := pdi;
Inc(tmppdi);
Result := px(pdi, tmppdi, n + 1, iCount);
if (n + 1 <> iCount - 1) then
jh(psi, pdi);
if n + 2 = iCount - 1 then
Result := True;
end
else
if n < iCount - 1 then
Result := True;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
iCount: Integer;
a: array of Integer;
begin
Memo1.Clear;
try
iCount := StrToInt(Edit1.Text);
//Edit中输入数组的大小
except
iCount := 100;
end;
//初始化数组
SetLength(a, iCount);
Randomize;
for i := 0 to iCount - 1do
a := Random(iCount * 2) + 1;
//输出原始数组
PrintIntArray(@a[0], iCount);
//排序:奇数在前,偶数在后,并且不改变原来的顺序
for i := 0 to iCount - 2do
begin
if px(@a, @a[i + 1], i, iCount) then
Break;
end;
//输出排序后的数组
PrintIntArray(@a[0], iCount);
end;
procedure TForm1.PrintIntArray(pi: PInteger;
ACount: Integer);
var
i: Integer;
begin
Memo1.Lines.begin
Update;
try
for i := 0 to ACount - 1do
begin
Memo1.Lines.Add(IntToStr(pi^));
Inc(pi);
end;
Memo1.Lines.Add('*******************************')
finally
Memo1.Lines.EndUpdate;
end;
end;