i:=1; while i<length(s) do begin if s[i] in [‘0’..’9′] then begin j:=i+1; while j<=length(s) do begin if s[j] in [‘0’..’9′] then begin val(s[i],so1); val(s[j],so2); if so1>so2 then begin tg:=s[i]; s[i]:=s[j]; s[j]:=tg; end; end; inc(j); end; end; inc(i); end; writeln(‘xau sau khi sap xep la: ‘,s); readln end.
program sapxepsotrongxau;
uses crt;
var s:string;
i,so1,so2,j:integer;
tg:char;
begin
clrscr;
write(‘Nhap xau: ‘); readln(s);
i:=1;
while i<length(s) do
begin
if s[i] in [‘0’..’9′] then
begin
j:=i+1;
while j<=length(s) do
begin
if s[j] in [‘0’..’9′] then
begin
val(s[i],so1);
val(s[j],so2);
if so1>so2 then
begin
tg:=s[i];
s[i]:=s[j];
s[j]:=tg;
end;
end;
inc(j);
end;
end;
inc(i);
end;
writeln(‘xau sau khi sap xep la: ‘,s);
readln
end.
Bài làm:
uses crt;
var s,t:string;
n,i,j,z,k,l:longint;
a:aray[1..100] of string;
begin
clrscr;
write(‘nhap xau:’);readln(s);
z:=1;j:=0;
s:=s+’ ‘;
for i:=1 to length(s) do
if (s[i]=’ ‘) and (s[i-1]<>’ ‘) then
begin
j:=j+1
a[j]:=”
for k:=z to i-1 do
begin
a[j]:=a[j]+s[k]
end;
z:=i+1;
end;
for i:=1 to j-1 do
for k:=i+1 to j do
if length(a[i])>length(a[k]) then
begin
t:=a[i];
a[i]:=a[k];
a[k]:=t;
end;
s:=”;
for i:=1 to j do
s:=s+ ‘ ‘+a[i];
writeIn(‘xau da sap xep tu tang dan la:’);
write(s);
readln;
end.
_
Bạn học tốt nhaa ·ω·