Sắp xếp dãy số chẵn trái tăng dần (giảm dần), lẻ phải giảm dần (tăng dần)
[You must be registered and logged in to see this link.]
- Code:
program sapxep;
uses crt;
type
ha=array[1..100]of integer;
var
n,i,j,t,dem:integer;
a:ha;
begin
write('Nhap so luong phan tu cua day so,N='); readln(n);
for i:=1 to n do
begin
write('phan tu thu ',i,' la:' ); readln(a[i]);
end;
dem:=0;
for i:=1 to n do
if a[i]mod 2=0 then dem:=dem+1;
for j:=n downto 2 do
for i:=1 to j-1 do
if (a[i]mod 2=1)and(a[i+1]mod 2=0) then
begin
t:=a[i]; a[i]:=a[i+1]; a[i+1]:=t;
end;
for j:=dem downto 2 do
for i:=1 to j-1 do
if a[i] > a[i+1] then
begin
t:=a[i]; a[i]:=a[i+1]; a[i+1]:=t;
end;
for j:=n downto dem+2 do
for i:=dem+1 to j-1 do
if a[i]<a[i+1] then
begin
t:=a[i]; a[i]:=a[i+1]; a[i+1]:=t;
end;
writeln('day so duoc sap xep la: ');
for i:=1 to n do write(a[i],' ');
readln
end.
[You must be registered and logged in to see this link.]