Sie sind auf Seite 1von 7

Ti liu n tp chuyn tin 11

Bi ton t mu
Uses Crt;
Const Max = !;
"i = #Tomau!$inp#;
%ar & ' &rray(1$$Max)1$$Max* o+ !$$1;
Mau),Mau ' &rray(1$$Max* o+ Byte;
-)i ' .nte/er;
0omauxu1un/)0oMauMax ' .nte/er;
2roce1ure Tao";
%ar i)3)x ' Byte;+ ' Text;
Be/in
&ssi/n4+)56; 7e8rite4+6;
7an1omi9e;
:riteln4+)Max6;
n '= Max;
"or i'=1 to n;1 1o
"or 3'=i<1 to n 1o
Be/in
x '= ran1om46;
.+ x =1 then :riteln4+)i'=)3'=6;
>n1;
Close4+6;
>n1;
2roce1ure -hap"ile;
%ar i)3 ' .nte/er;
" ' Text;
Be/in
"illChar4&)0i9eo+4&6)!6;
&ssi/n4")"i6; 7eset4"6;
7ea1ln4")-6;
:hile not >o+4"6 1o
Be/in
7ea14")i6;
:hile not eoln4"6 1o
Be/in
7ea14")36;
1
Ti liu n tp chuyn tin 11
&(i)3* '= 1; &(3)i* '= 1;
>n1;
7ea1ln4"6;
>n1;
>n1;
2roce1ure ?ien;
%ar i)3 ' .nte/er;
Be/in
:riteln;
"or i'=1 to - 1o
Be/in
"or 3'=1 to - 1o :rite4&(i)3*'=6;
:riteln;
>n1;
>n1;
2roce1ure @hoitri;
Be/in
"illChar4Mau)si9eo+4Mau6)!6;
0oMauMax '= -;
0omauxu1un/ '= 1;
Mau(1* '= 1;
>n1;
"unction @t4x)m ' .nte/er6' Boolean;A Mau m /an cho 1inh x B
Be/in
"or i'=1 to - 1o
.+ 4&(x)i*=16 an1 4m=Mau(i*6 then
Be/in @t '= "alse;>xit;>n1;
@t '= True;
>n1;
2roce1ure Tomau4x ' .nte/er6; A To mau cho 1inh x B
%ar m)luu),uumaux ' .nte/er;
Be/in
.+ x=-<1 then
Be/in
,Mau '= Mau;

Ti liu n tp chuyn tin 11


0oMauMax '= 0omauxu1un/;
>xit
>n1;
m '= 1;
:hile mC=0oMauMax 1o
Be/in
.+ 4@T4x)m66 then
Be/in
,uuMaux '= Mau(x*;
Mau(x* '= m;
,uu '= 0omauxu1un/;
.+ 0omauxu1un/Cm then 0omauxu1un/ '= m;
Tomau4x<16;
0omauxu1un/ '= ,uu;
Mau(x* '= ,uuMaux;
>n1;
.nc4m6;
>n1;
>n1;
2roce1ure Thon/Dao;
%ar i ' .nte/er;
Be/in
"or i'=1 to - 1o
:riteln4 # Eiem #)i')# to mau ' #),Mau(i*6;
>n1;
B>F.-
Clrscr;
A Tao";B
-hap"ile;
?ien;
@hoitri;
Tomau46;
Thon/Dao;
>-E$
Cch 2 : Greedy t mu nh no c bc cao trc
G
Ti liu n tp chuyn tin 11
Uses Crt;
Const Max = 1!!;
"i = #TomauG$inp#;
"o = #Tomau$out#;
Type @M = &rray(1$$Max* o+ Byte;
@& = &rray(1$$Max)1$$Max* o+ !$$1;
%ar M)B),' @M;
& ' @&;
- ' Byte;
Procedure NhapF;
%ar i)3 ' Byte;
" ' Text;
Be/in
&ssi/n4")"i6;
AH.;B7eset4"6;AH.<B
.+ .o7esultCI! then
Be/in
:riteln4#,oi "ile #)"i6;
7ea1ln;
?alt;
>n1;
7ea1ln4")-6;
"or i'=1 to - 1o
"or 3'=1 to - 1o &(i)3* '= !;
"or i'=1 to - 1o B(i* '= !;
:hile -ot eo+4"6 1o
Be/in
7ea14")i6;
:hile not eoln4"6 1o
Be/in
7ea14")36;
&(i)3* '= 1;
&(3)i* '= 1;
.nc4B(i*6; ABc cJa KLnh i B
.nc4B(3*6; ABc cJa KLnh 3 B
>n1;
7ea1ln4"6;
=
Ti liu n tp chuyn tin 11
>n1;
Close4"6;
>n1;
Procedure HienL;
%ar i ' Byte;
Be/in
:riteln;
"or i'=1 to - 1o :rite4,(i*'=6;
>n1;
Procedure Sapxep; { Sp xp cc nh theo bc ca
nh }
%ar coc)i)3 ' Byte;
Be/in
"or i'=1 to - 1o ,(i* '= i;
"or i'=1 to -;1 1o
"or 3'=i<1 to - 1o
.+ B(i*CB(3* then
Be/in
coc '= B(i*;
B(i* '= B(3*;
B(3* '= coc;
coc '= ,(i*; A ,(i* l tn KLnh thM i sau Nhi
sOp xPp B
,(i* '= ,(3*;
,(3* '= coc;
>n1;
>n1;
Procedure o!au;
%ar i)1)mau ' Byte;

Function "on#$ %oo&ean;A@iQm tra cRn KLnh no cha K;
Sc t mu B
%ar i ' Byte;
Be/in
Ton/ '= "alse;
"or i'=1 to - 1o
U
Ti liu n tp chuyn tin 11
.+ M(i*=! then >xit;
Ton/ '= True;
>n1;

Function 'hapnhan()*i $ %+te, $ %oo&ean;
%ar N ' Byte;
Be/in
Chapnhan '= "alse;
"or N'=3 to i;1 1o
.+ 4&(,(N*),(i**=16 an1 4M(,(N**=mau6 then >xit;
Chapnhan '= True;
>n1;
Begin
"or i'=1 to - 1o M(i* '= !;
1 '= 1;
mau '= !;
7epeat
.nc4mau6;
M(,(1** '= mau;
"or i'=1<1 to - 1o
.+ 4&(,(1*),(i**=!6 then
.+ Chapnhan41<1)i6 then M(,(i** '= mau;
:hile 4M(,(1**I!6 an1 41C-6 1o inc416;
Until xon/;
:riteln4#0omau = #)mau6;
End;
Procedure Hien-;
%ar i ' Byte;
Be/in
:riteln;
"or i'=1 to - 1o :rite4i'=6;
:riteln;
"or i'=1 to - 1o :rite4M(i*'=6;
>n1;
BEG!
V
Ti liu n tp chuyn tin 11
Clrscr;
-hap";
0apxep;
Tomau;
?ienM;
E!"#
W

Das könnte Ihnen auch gefallen