# Một số bài tập Pascal

11TYPE mang1=array[1..100] of integer; mang2=array[1..50,1..50] of integer; CONST a:array[0..15] of !ar="#0#,#1#,#2#,#\$#,#%#,#5#,#&#,#'#,#(#,#)#,#*#,#+#,#C#,#,#,#E#,#-#.; /*0 a, 1:mang1; :mang2; 2,i,3,4,f1,f2,min,ma4,5t:integer; 6,r,t,y, o2e,7o28:integer; f:te4t;9a:array[0..)] of integer; 6t:1oo:ean; 7,7t,71,72,a1,11:7tring; fi::ongint;n,m,;,<:integer; e4;:rea:; begin {Kiem tra mot day la cap so cong} write('Nhap n:');readln(n); for i:= to n do begin write('Nhap a!'"i"'#:');readln(a!i#); end; \$t:=tr%e;i:=&; d:=a!&#'a! #; while (i(=n) do begin if a!i) #'a!i# (*d then \$t:=false; inc(i); end; if \$t then write('da+ la cap so cong') else write(',hong phai la cap so cong'){In so nhi phan cua mot so n} 9=!ong 2e <8y> f%nction nhiphan(n:integer):.tring; /ar i:integer;st:string; begin st:=''; while (n*0) do begin str(n 1od &"s ); st:=s )st; n:=n di/ &; end; nhiphan:=st; end; 9,e <8y> f%nction de2%+( n:integer):string; /ar s :string; begin if n=0 then de2%+:='' else begin str((n 1od &)"s ); de2%+:=de2%+(n di/ &))s ; end;

Một số bài tập Pascal
end; begin write('Nhap n:');readln(n); write('3a% nhi phan la '"de2%+(n)); {Dem so 0 co nghia} 9=!ong 2e <8y> while n*0 do begin if n 1od 0 =0 then inc(d); n:=n di/ 0; end; write('.o ch% so 0 co nghia la'"d);9,e <8y> f%nction de2%+(n:longint):b+te; begin if 4(n=0) or (n= ) 4 n di/ 0 =0- then de2%+:=0 else if n 1od 0=0 then de2%+:=de2%+(n di/ 0)) 9?8 :y tr8ong !o; 7o 0 8oi 8ng> else de2%+:=de2%+(n di/ 0); end; {Dem so} 9,e <8y> f%nction de2%+(n:integer):integer; begin if n di/ 0=0 then de2%+:= else de2%+:=de2%+(n di/ 0)) ; end; 9=!ong 2e <8y> begin write('nhap n:');readln(n); d:=0; while n*0 do begin d:=d) ; n:=n di/ 0; end; write('so co nghia la'"d); readln; end5 /ar s:string;n"i"6:integer; a:arra+! 55 00# of string; {Xau dao} f%nction lap(s:string):string; /ar i:integer;st:string; begin st:=''; for i:=length(s) downto do st:=st)s!i#;

&

Một số bài tập Pascal
lap:=st; end; f%nction de2%+(s:string):string; begin if length(s)( then de2%+:=s else de2%+:=s!length(s)#)de2%+(cop+(s" "length(s)' )); end; {Xau doi xung} f%nction \$t(s:string):boolean; /ar i:integer;st:string; begin st:=''; for i:=length(s) downto do st:=st)s!i#; if st=s then \$t:=tr%e else \$t:=false; end; f%nction de2%+dao(s:string):boolean; begin if length(s)( then de2%+dao:=tr%e else if s! #(*s!length(s)# then de2%+dao:=false else begin delete(s" " ); delete(s"length(s)" ); de2%+dao:=de2%+dao(s); end; end; begin write('Nhap 7a%:');readln(s); writeln('3a% dao la '"lap(s)); writeln('7a% dao la '"de2%+(s)); if \$t(s) then writeln('3a% doi 7%ng') else writeln('3a% \$hong doi 7%ng'); if de2%+dao(s) then writeln('3a% doi 7%ng') else writeln('3a% \$hong doi 7%ng');writeln('Nhap so hoc sinh:');readln(n); readln; end5 {Tinh tong p=anxn+an-1xn-1+ +a!x!+a1x1+a0 hay p=anxn-1+ +ax+a0 tuy "ao "iec nhap n} d:=a!n#; for i:=n' downto 0 do d:=d87)a!i#; write('9ong la'"d);

:

Một số bài tập Pascal
{#iet chuong trinh tinh so \$i%onaci thu n "iet "ao \$ile} ;rite('Nhap ten file:');readln(s); assign(f"s); rewrite(f); ;rite('<an can ti1 so fibonaci th%:');readln(n); f := ;f&:= ;writeln(f"f );writeln(f"f&); for i:=: to n do begin fi:=f )f&; writeln(f"fi"' '); f :=f&; f&:=fi; end; close(f); assign(f"s); reset(f); 9,o 2ay fi1ona i> while not eof(f) do begin readln(f"7); writeln(7); end; {Tong %inh phuong cac so cua n} 9=!ong 2e <8y> d:=0; while n*0 do begin d:=d)s2r(n 1od 0); n:=n di/ 0; end;9,e <8y> f%nction de2%+(n:integer):integer; begin if n =0 then de2%+:=0 else de2%+:=de2%+(n di/ 0))s2r(n 1od 0); end; begin write('Nhap n:');readln(n); write('9ong la '"de2%+(n)); {Xoa& chen mot phan tu trong mang} 9+pe 1ang=arra+! 55 00# of integer; /ar a:1ang; i"6"7"n:integer; proced%re 7oa(/ar a:1ang;/ar n:integer;7:integer); /ar i:integer; begin i:= ; while (i(=n) and (a!i#(* 7) do inc(i); for 6:=i to n' do

=

Một số bài tập Pascal
a!6#:=a!6) #; n:=n' ; end; proced%re chen(/ar a:1ang;/ar n:integer;7:integer); /ar i:integer; begin i:= ; while (i(=n) and (a!i#(7) do inc(i); for 6:=n) downto i do a!6#:=a!6' #; a!6#:=7; n:=n) ; end; begin write('Nhap n:');readln(n); write('Nhap 7:');readln(7); for i:= to n do begin write('Nhap a!'"i"'#:');readln(a!i#); end; 7oa(a"n"7); writeln('>a+ sa% \$hi 7oa la'); for i:= to n do write(a!i#); writeln; chen(a"n"7); writeln('>a+ sa% \$hi chen la'); for i:= to n do write(a!i#); readln; end5 {Doan dai nhat gom cac so 0 lien tiep} de1:=0; for i:= to n do begin if a!i# =0 then de1:=de1) else de1:=0; if de1*1a7 then 1a7:=de1; end; write('>oan dai nhat go1 cac so 0 lien tiep la '"1a7); readln; end5 {Tim doan dai nhat co tong lien tiep tang trong day} /ar a:arra+! 55 00# of integer; n" i"6"tong"1a7"/t:integer; begin write('Nhap so phan t% c%a 1ang:');readln(n); for i:= to n do begin write('Nhap a!'"i"'#:');readln(a!i#); end; i:= ;1a7:=0;

?

Một số bài tập Pascal
while (i(=n) do begin 6:=i) ;tong:=a!i#; while (6(=n) and (a!6#*a!6' #) do begin tong:=tong)a!6#; if tong*1a7 then begin 1a7:=tong; /t:=i; end; inc(6); end; tong:=0; inc(i); end; writeln('>a+ co tong tang lon nhat'); while (/t(=n) and (a!/t#(a!/t) #) do begin write(a!/t#); /t:=/t) ; end; writeln(a!/t#); write('@oi tong lon nhat la '"1a7); readln; end5 {Tron hai mang duoc sap xep tang dan thanh mot mang duoc sap xep tang dan} ;riteAN('NBCP PBCN 9D EBF MCNG C:'); ;riteln('HHHHHHHHHHHHHHHHHHHHHHHHH'); write('Nhap n:');readln(n); for i:= to n do begin write('Nhap a!'"i"'#:');readln(a!i#); end; ;IJteln('NBCP PBCN 9D EBF MCNG <:'); write('Nhap 1:');readln(1); for i:= to 1 do begin write('Nhap b!'"i"'#:');readln(b!i#); end; i:= ;6:= ;\$:= ; 9Ca !ai 2ay !8a !et> while (i(=n) and (6(=1) do begin if a!i#(b!6# then begin c!\$#:=a!i#; inc(i); end else

K

Một số bài tập Pascal
begin c!\$#:=b!6#; inc(6); end; inc(\$); end; 9,ay 1 !et> while (i(=n) do begin c!\$#:=a!i#; inc(i); inc(\$); end; 9,ay a !et> while (6(=1) do begin c!\$#:=b!6#; inc(6); inc(\$); end; ;IJ9LAN('>CM .CD ,BJ 9IFN AC'); writeln('HHHHHHHHHHHHHHHHHHH'); for \$:= to n)1 do write(c!\$#"' ') ;{Doi cho phan tu lon nhat "a nho nhat} write('Nhap n:');readln(n); for i:= to n do begin write('Nhap a!'"i"'#:');readln(a!i#); end; i:= ;6:=n;1in:=a! #;1a7:=a!n#; while i(=n do begin if a!i#(=1in then begin 1in:=a!i#; \$:=i; end; inc(i); end; while 6*= do begin if a!6#*= 1a7 then begin 1a7:=a!6#; 7:=6; end; dec(6); end; a!\$#:=1a7;

N

Một số bài tập Pascal
a!7#:=1in; for i:= to n do write(a!i#) {Tao day sao cho a'i+1(-a'i()=* luu y %ai nay +ho phan %iet duoc& random,*- chi lay gia tri tu 0 den .} a! #:=rando1(K); for i:=& to =000 do a!i#:=a!i' #)rando1(K)) ; for i:= to =000 do write(a!i#"' ');{/ho n&m dua ra man hinh so chu so xuat hien trong m+n} write('Nhap 1:');readln(1); write('Nhap n:');readln(n); \$:=1)n; writeln('.o can de1 la'"\$); for i:=0 to O do a!i#:=0; 4,hoi tao 1ang de1 co gia tri bang 0while \$*0 do begin inc(a!\$ 1od 0#); \$:=\$ di/ 0; end; for i:=0 to O do if a!i#*= then writeln('so lan 7%at hien ch% so'"i"':'"a!i#);{Xac dinh ngoac dung sai ,---} write('Nhap 7a%:');readln(s); i:= ;1:=0; while i(=length(s) do begin if s!i#='(' then inc(1) else dec(1); inc(i); end; if 1=0 then writeln('<an da dat ngoac d%ng') else write('<an da dat ngoac sai'); {Tinh tong ca so nam giua so 0 dau tien "a so 0 cuoi cung neu hai so 0 nam canh nhau thi +et 0ua se la 0} write('Nhap n:');readln(n); for i:= to n do begin write('Nhap a!'"i"'#:');readln(a!i#); end; i:= ;6:=n; 7:=0; while (i(=n) and (a!i#(*0) do inc(i); while (6*= ) and (a!6#(*0) do dec(6); if (6'i)*=& then begin for \$:=i to 6 do 7:=7)a!\$#; end

P

Một số bài tập Pascal
else writeln('9ong la 0'); writeln('9ong la '"7);{Dem so tu +hi co cac +i tu phan cach & } d:=0; write('Nhap 7a%:');readln(s); d:= ;i:= ; while i(=length(s) do begin if (s!i#=' ') and (s!i) #(*' ') or ((s!i#='"') and (s!i) #=' ') and (s!i)&#(*' ')) or ((s!i#='5') and (s!i) #=' ') and (s!i)&#(*' ')) then d:=d) ; if s!i#='"' then i:=i)& else i:=i) ; end; write('so t% la '"d);{ 1ien thi tu dai nhat co trong xau} /ar s:string; i"6"1a7"d:integer; proced%re 7oa(/ar s:string); begin while pos(' '"s)(*0 do delete(s"pos(' '"s)" ); if s! #=' ' then delete(s" " ); if s!length(s)#=' ' then delete(s"length(s)" ); end; f%nction t%dai(s:string):string; /ar t%"1a7:string;de1:integer; begin s:=s)' ';1a7:=''; while pos(' '"s)(* 0 do begin t%:=cop+(s" "pos(' '"s)' ); if length(t%)*length(1a7) then 1a7:=t%; delete(s" "pos(' '"s)); end; t%dai:=1a7; end; begin write('Nhap 7a%:');readln(s); write('9% dai nhat la '"t%dai(s)"Q /oi do dai la Q"length(t%dai(s))); readln; end5 { Thay the cac tu 2anh3 thanh tu 2em3 co trong xau s} /ar s:string; begin write('Nhap 7a%:');readln(s); while pos('anh'"s)(* 0 do begin

O

Một số bài tập Pascal
insert('e1'"s"pos('anh'"s)); delete(s"pos('anh'"s)":); end; writeln('3a% 1oi la '"s);readln; end5 {Tach xau so cuoi cung co trong xau} /ar s"st:string; i"6:integer; begin write('Nhap 7a%:');readln(s); i:=length(s); while (i*= )and not (s!i# in !'0'55'O'#) do dec(i); 6:=i; while (6*= ) and (s!6# in!'0'55'O'#) do dec(6); st:=cop+(s"6) "i'6); write(R3a% so c%oi c%ng laQ"st); readln; end5 {4hap "ao phan tu cua day "oi dau hieu +et thuc la 0} begin i:= ; repeat write('Nhap a!'"i"'#:');readln(a!i#); inc(i); %ntil a!i' #=0; d:=0; for \$:= to i'& do begin d:=d) ; write(a!\$#"' '); end; writeln; write('.o l%ong cac so da nhap la '"d); {Tim doan dai nhat chia het mot so + cho truoc trong mot mang "a "iet "ao \$ile} begin write('Nhap n:');readln(n); write('Nhap \$:');readln(\$);write('Nhap ten file:');readln(s);4 for i:= to n do begin write('Nhap a!'"i"'#:');readln(a!i#); end; assign(f"s); 4Tao fi:e> rewrite(f); writeln(f"n"' '"\$); for i:= to n do begin if i=n then write(f"a!i#) else

0

Một số bài tập Pascal
write(f"a!i#"' '); end; close(f); 9Tim 2oan 2ai n!at !ia !et !o mot 7o 6 !o tr8o > i:= ; d:=0;1a7:=0; while i(=n do begin 6:=i; while (6(=n) and (a!6# 1od \$=0) do begin d:=d) ; if 1a7(d then begin 1a7:=d; /t:=i; end; inc(6); end; i:=i) ; d:=0; end; while (/t(=n) and (a!/t# 1od \$ =0) do begin write(a!/t#"' '); /t:=/t) ; end; write('>oan dai nhat la'"1a7); 9T8 fi:e 5iet ra mang> assign(f"s); reset(f); readln(f"n"1);writeln(n"1); r:= ; while not eof(f) do begin read(f"7); a!r#:=7; r:=r) ; end; close(f); for t:= to r' do write(a!t#); writeln; i:= ; d:=0;1a7:=0; while i(=r' do begin 6:=i; while (6(=r' ) and (a!6# 1od \$=0) do begin d:=d) ; if 1a7(d then begin

Một số bài tập Pascal
1a7:=d; /t:=i; end; inc(6); end; i:=i) ; d:=0; end; while (/t(=n) and (a!/t# 1od \$ =0) do begin write(a!/t#"' '); /t:=/t) ; end; writeln; writeln('>oan dai nhat la '"1a7); {Xoa cac so nam giua x "a y} 9Ca ! 1 28ng mang 1 5iet <8a> proced%re 7oa(/ar a:1ang;n"7"+:integer;/ar 1:integer); /ar b:1ang; i"6:integer; begin 1:= ; i:= ; while (i(=n) do begin if (a!i#(7)or (a!i#*+) then begin b!1#:=a!i#; 1:=1) ; end; inc(i); end; a:=b;1:=1' ; end; 9Ca ! 2> proced%re 7oa(/ar a:1ang;/ar n:integer;7:integer); begin i:= ; while a!i#(*7 do inc(i); for 6:=i to n' do a!6#:=a!6) #; n:=n' ; end; proced%re 7oathiet(/ar a:1ang;7"+"n:integer;/ar 1:integer); begin i:= ; while (i(=n) do begin if (a!i#*=7) and (a!i#(=+) then 7oa(a"n"a!i#) else inc(i); 1:=n; end; end; begin

&

Một số bài tập Pascal
write('Nhap 7:');readln(7); write('Nhap +:');readln(+); write('Nhap n:');readln(n); for i:= to n do begin write('Nhap a!'"i"'#:');readln(a!i#); end; 7oathiet(a"7"+"n"1); writeln('Eac so sa% \$hi 7oa la'); for i:= to 1 do write(a!i#);{4hap xau& thong +e so chu so xuat hien trong xau dieu +ien nhap "ao la +i tu dau +hac 0} begin repeat write('Nhap 7a%:'); readln(s); %ntil s! #(*'0'; st:=''; 9Ca ! 1> for i:=0 to O do a!i#:=0; for 6:= to length(s) do begin /al(s!6#"1"7); a!1#:=a!1#) ; end; for i:=0 to O do if a!i#*0 then begin str(a!i#"s ); str(i"s&); st:=st)s )s&; end;9Ca ! 2> i:= ; d:= ; while (i(=length(s)) do begin 6:=i) ; while 6(=length(s) do begin if s!6#=s!i# then begin d:=d) ; delete(s"6" ); end else inc(6); end; str(d"s ); st:=st)s )s!i#; inc(i);

:

Một số bài tập Pascal
d:= ; end; write('3a% thong \$e la '"st);{/ho ma 1* cua so n cho truoc} begin write('Nhap n:');readln(n); st:=''; if n=0 then st:='0'; while n*0 do 9 a ! 1> begin st:=a!n 1od K#)st; n:= n di/ K; end;; 9Ca ! 2> begin if (n 1od K(=O) then begin str(n 1od K"s ); st:=s )st ; end else begin case (n 1od K) of 0:st:='C')st; :st:='<')st; &:st:='E')st; ::st:='>')st; =:st:='L')st; ?:st:='S')s; end; end; n:=n di/ K; end; writeln('7a% thong \$e la:'"st);{xoa chan& xoa le& xoa chan& xoa le--5 con 1 so} 9+pe 1ang=arra+! 55 0# of integer; /ar s:string; a:1ang; i"6"7"code:integer; n:longint; proced%re ch%+en(s:string;/ar a:1ang; /ar n:longint); /ar i:integer; begin n:=length(s); for i:= to n do /al(s!i#"a!i#"code); end;

=

Một số bài tập Pascal
proced%re 7oa(/ar a:1ang;/ar n:longint;7:integer); /ar i"6:integer; begin i:= ; while (i(=n) and (a!i#(*7) do inc(i); for 6:=i to n' do a!i#:=a!i) #; n:=n' ; end; proced%re 7oachan(/ar a:1ang;/ar n:longint); /ar i:integer; begin i:= ; while (i(=n) do begin if i 1od &=0 then 7oa(a"n"a!i#); inc(i); end; end; proced%re 7oale(/ar a:1ang;/ar n:longint); /ar 6:integer; begin 6:= ; while (6(=n) do begin if 6 1od & (*0 then 7oa(a"n"a!6#); inc(6); end; end; f%nction 7oathiet(/ar a:1ang; n:longint):integer; begin while n(* do begin 7oachan(a"n); if n= then 7oathiet:=a!n# 9 Tr8ong !o; 4oa !an 4ong !i on 1 7o> else 7oale(a"n); end; 7oathiet:=a!n#; end; begin write('Nhap 7a%:');readln(s); ch%+en(s"a"n); writeln('3a% \$et 2%a la '"7oathiet(a"n)); readln; end5

?

Một số bài tập Pascal
{Thong +e 666777///DD889--5:6:7:/!D!8\$} begin write('Nhap 7a% can thong \$e:');readln(s); st:=''; i:= ; while i(=length(s) do begin 6:=i) ; d:= ; while (6(=length(s)) and (s!i#=s!6#) do begin d:=d) ; inc(6); end; if d* then begin str(d"s ); st:=st)s )s!i#; end else st:=st)s!i#; i:=6; end; write('3a% sa% \$hi thong \$e la'"st); {;a hoa theo ma Xeda} const st:string='abcdefghi6\$l1nop2rst%/w7+Tabcdefghi6\$l1nop2rst%/w7+T'; sh:string='C<E>LSGBJU,AMNFPVI.9D@;3MWC<E>LSGBJU,AMNFPVI.9D@;3 MW'; /ar s:string; \$"i:integer; ch:char; begin repeat write('Nhap 7a% can 1a hoa:');readln(s); write('Nhap so so lan:');readln(\$); for i:= to length(s) do case s!i# of 'a'55'T': s!i#:=st!pos(s!i#"st))\$#; 'C'55'W': s!i#:=sh!pos(s!i#"sh))\$#; end; writeln('3a% 1a hoa la:'"s); writeln('<an co 1%on la1 tiep \$hong:(cX\$)');readln(ch); %ntil ch='\$'; readln; end5

K

Một số bài tập Pascal
{Tim hoan "i lon nhat cua mot so n cho truoc} f%nction hoan/i(n:integer):string; /ar i:integer;s:string;tg:char; begin str(n"s); for i:= to length(s) do for 6:=i) to length(s) do if s!i#(s!6# then begin tg:=s!i#;s!i#:=s!6#;s!6#:=tg;end; hoan/i:=s; end; <egin write('Nhap n:');readln(n); write('Boan /i lon nhat la'"hoan/i(n)); readln; end5 {<iet +e tat ca cac hoan "i cua n} /ar 7:arra+! 55 0# of b+te; \$t:arra+! 55 0# of boolean; i"de1"n:b+te; proced%re in\$2; /ar i:b+te; begin inc(de1); for i:= to n do write(7!i#); writeln; end; proced%re tr+(6:b+te); /ar i:b+te; begin for i:= to n do if \$t!i# then begin 7!6#:=i; \$t!i#:=false; if 6=n then in\$2 else tr+(6) ); \$t!i#:=tr%e; end; end; begin write('Nhap n:');readln(n); for i:= to n do \$t!i#:=tr%e; de1:=0; tr+( ); writeln('.o hoan /i la '"de1); readln; end5

N

Một số bài tập Pascal
{<iet +e cac so nguyen to co trong day=>} begin write('Nhap n:');readln(n); for i:= to n do begin write('Nhap a!'"i"'#:');readln(a!i#); end; 9 a ! 1> i:= ; while (i(=n) do begin d:=0; for 6:= to a!i# do if a!i# 1od 6= 0 then inc(d); if d=& then writeln(a!i#"'la so ng%+en to'); inc(i); end; 9 a ! 2> i:= ; while (i(=n) do begin 6:=&; while (a!i# 1od & (*0) and (6(=ro%nd(s2rt(a!i#))) do inc(6); if 6*ro%nd(s2rt(a!i#)) then writeln(a!i#"'la so ng%+en to'); inc(i); end; 9@iet 6e a 7o !oan !ao o trong 2ay> i:= ; while (i(=n) do begin 7:=0; for 6:= to (a!i# di/ &) do if a!i# 1od 6=0 then 7:=7)6; if 7=a!i# then write(a!i#"' Aa so hoan hao'); inc(i); end; {/ong hai xau "oi nhau} begin write('Nhap 7a% s :');readln(s ); write('Nhap 7a% s&:');readln(s&); st:=''; sod%:=0; if length(s )*length(s&) then begin for i:= to length(s )'length(s&) do s&:='0')s&; end; if length(s&)*length(s ) then begin for 6:= to length(s&)'length(s ) do s :='0')s ; end; for i:=length(s ) downto do begin

P

Một số bài tập Pascal
/al(s !i#"7"code); /al(s&!i#"+"code); c:=(7)+)sod%) 1od 0; sod%:=(7)+)sod%) di/ 0; str(c"a ); st:=a )st; end; 9?8 :y tr8ong !o; 7o 28 8oi 8ng> if sod%*0 then begin str(sod%"b ); st:=b )st; end; writeln('3a% \$et 2%a la'"st); {Xoa trang} proced%re 7oatrang(/ar s:string); begin while pos(' '"s)(* 0 do delete(s"pos(' '"s)" ); if s! #=' ' then delete(s" " ); if s!length(s)#=' ' then delete(s"length(s)" ); end; begin write('nhap 7a%:');readln(s); {Dem so +i tu +hac nhau} proced%re de1(/ar s:string); /ar i"6:integer; begin i:= ; while (i(=length(s) )do begin 6:=i) ; while (6(=length(s)) do begin if s!6#=s!i# then delete(s"6" ) else inc(6); end; inc(i); end; end; <egin write('Nhap 7a%:');readln(s); de1(s); writeln('.o \$i t% \$hac nha% la '"length(s));{ ;o ta ham sumdigit,n- tra "e tong cac chu so cua n sau do "an dung} f%nction s%1digit(n:longint):integer; begin if n=0 then s%1digit:=0 else s%1digit:=s%1digit(n di/ 0))n 1od 0;

O

Một số bài tập Pascal
end; {Tinh giai thua cua mot so} f%nction gt(n:longint):longint; begin if n=0 then gt:= else gt:=gt(n' )8n end; {;o ta ham 8mu,x&exp- trr "e gia tri ex theo cong thuc {ex=1+x+x!?!@ do chinh xac xn?n@)esp} f%nction e1%(7:integer;e7p:real):real; /ar a"n:integer;tong:real; begin n:= ;a:=7;tong:= ; while (aXgt(n)*=e7p) do begin tong:=tong)aX(gt(n)); a:=a87; n:=n) ; end; e1%:=tong; end; begin write('Nhap 7:');readln(7); write('Nhap e7p:');readln(e7p); writeln('Ba1 e 1% 7 la '"e1%(7"e7p):&:&); {;o ta ham pos+,x&y&+- tra "e gia tri A la"i tri xut hien lan thu + cua xau s1 trong xau s!} f%nction pos\$(s "s&:string;\$:integer):integer; /ar i"de1"/t:integer; <egin /t:=0;de1:=0; while (pos(s "s&)*0) do begin de1:=de1) ; /t:=/t)pos(s "s&); if de1=\$ then pos\$:=/t else s&:=cop+(s&"pos(s "s&)) "length(s&)); end; end; <egin write('Nhap 7a% s :');readln(s ); write('Nhap 7a% s&:');readln(s&); ;rite('Nhap so lan 7%at hien:');readln(\$); writeln('.o lan 7%at hien c%a 7a% s trong 7a% s& lan th% '"\$"' la '"pos\$(s "s&"\$));-

&0

Một số bài tập Pascal
{4hap "ao so thuc x& tinh "a dua ra man hinh so nguyen n nho nhat thoa man dieu +ien x)tong 1?i&i=1 n} f%nction nhonhat(7:integer):integer; /ar n:integer;tong:real; begin n:= ;tong:= ; while (tong(=7) do begin n:=n) ; tong:=tong) Xn; end; nhonhat:=n; end; begin write('Nhap 7:');readln(7); ;rite('n nho nhat thoa 1an die% \$ien bai toan la'"nhonhat(7)); {Tim so nguyen + nho nhat thoa man dieu +en :mu+5n duoc nhap tu %an phim} f%nction 1%be(n:integer):integer; /ar d"\$:integer; begin d:= ;\$:=0; while d(n do begin d:=d8:; inc(\$); end; 1%be:=\$; end; begin write('Nhap n:');readln(n); writeln('.o ng%+en \$ nho nhat la '"1%be(n)); {;ot so %ai toan tren mang ! chieu} {tong chinh} f%nction tongchinh(1:1ang&;n:integer):integer; /ar i"d: integer; begin d:=0; for i:= to n do d:=d)1!i"i#; tongchinh:=d; end; {Tong phu} 4f%nction tongph%(1:1ang&;n:integer):integer; /ar i"d:integer; begin d:=0; for i:= to n do d:=d)1!i"n'i) #; tongph%:=d;

&

Một số bài tập Pascal
end; proced%re swap(/ar 7"+:integer); /ar tg:integer; begin tg:=7; 7:=+; +:=tg; end; proced%re ch%+en/i(/ar c:1ang&;n:integer); /ar i"6:integer; begin for i:= to n do for 6:= to n do if i(6 then swap(c!i"6#"c!6"i#); 9!oa iA3 2e tran! tin! trang 2oi 2 :an t!an! ra 6!ong 2oi> end; begin write('Nhap so phan t% c%a 1ang & chie% /%ong:');readln(n); for i:= to n do for 6:= to n do begin write('Nhap c!'"i"'"'"6"'#:'); readln(c!i"6#); end; 9Bn mang t!eo 2ang ma tran> for i:= to n do begin for 6:= to n do write(c!i"6#"' '); writeln; end; writeln('9ong cac phan t% tren d%ong cheo chinh la '"tongchinh(c"n)); writeln('9ong cac phan t% tren d%ong cheo ph% la '"tongph%(c"n)); ch%+en/i(c"n); writeln('Ma tran ch%+en /i la'); 4<i loi ch%a d%ngfor i:= to n do begin for 6:= to n do write(c!i"6#"' '); writeln; end; {Xay dung thu tuc sBapline,c&m&n&i&Adoi cho hai dong i "a A cua mang c} proced%re swap(/ar 7"+:integer); /ar tg:integer; begin tg:=7; 7:=+; +:=tg; end;

&&

Một số bài tập Pascal
Proced%re swapline(/ar c:1ang&;1"n"i"6:integer); /ar \$:integer; begin for \$:= to n do swap(c!i"\$#"c!6"\$#); end; {Xay dung thu tuc sBapline,c&m&n&p&0doi cho hai cot p "a 0 cua mang c} proced%re swapcol(/ar c:1ang&;1"n"p"2:integer); /ar \$:integer; begin for \$:= to 1 do swap(c!\$"p#"c!\$"2#); end; begin write('Nhap so hang c%a 1ang:');readln(1); write('Nhap so cot c%a 1ang:');readln(n); for i:= to 1 do for 6:= to n do begin write('Nhap c!'"i"'"'"6"'#:');readln(c!i"6#); end; for i:= to 1 do begin for 6:= to n do write(c!i"6#"' '); writeln; end; write('Nhap so hang th% nhat can doi:');readln(7); write('Nhap hang th% hai can doi:');readln(+); swapline(c"1"n"7"+); writeln('Mang sa% \$hi doi hang'"7"'/a hang'"+"'la'); for i:= to 1 do begin for 6:= to n do write(c!i"6#"' '); writeln; end; write('Nhap so cot th% nhat can doi:');readln(p); write('Nhap cot th% hai can doi:');readln(2); swapcol(c"1"n"p"2); writeln('Mang sa% \$hi doi cot'"p"'/a cot'"2"'la'); for i:= to 1 do begin for 6:= to n do write(c!i"6#"' '); writeln; end; begin 9tao fi:e !8a mang 2 !ie8 t!eo 2ang ma tran> write('Nhap ten file :');readln(s); assign(f"s); rewrite(f); write('Nhap so phan t% c%a 1ang & chie% /%ong:');readln(n); writeln(f"n);

&:

Một số bài tập Pascal
for i:= to n do for 6:= to n do begin write('Nhap c!'"i"'"'"6"'#:');readln(c!i"6#); if 6(n then write(f"c!i"6#"' ') else writeln(f"c!i"6#); end;

9,o fie 5iet ra man !in! 28oi 2ang ma tran> reset(f); readln(f"n);writeln(n);i:= ; 9Ca ! 1> while not eof(f) do begin if i=n then begin readln(f"7); writeln(7); i:=0; end else begin read(f"7); write(7"' '); end; inc(i); while not eoln(f) do 9Ca ! 2> begin read(f"7); write(7); end; readln(f); writeln;9Bn ma tran !8yen 5i> <egin write('Nhap so phan t% c%a 1ang & chie% /%ong:'); readln(n); for i:= to n do for 6:= to n do begin write('Nhap c!'"i"'"'"6"'#:'); readln(c!i"6#); end; writeln('Jn 1a tran l%c nhap'); for i:= to n do begin for 6:= to n do

&=

Một số bài tập Pascal
write(c!i"6#"' '); writeln; end; ch%+en/i(c"n); writeln('Jn 1a tran ch%+en /i'); for i:= to n do begin for 6:= to n do write(c!i"6#"' '); writeln; end; 9/iet ma tran !8yen 5i 5ao fi:e> 4writeln('@iet 1a tran ch%+en /i /ao file'); writeln('Nhap ten file:');readln(s); assign(f"s); rewrite(f); writeln(f"n); for i:= to n do for 6:= to n do if 6=n then writeln(f"c!i"6#) else write(f"c!i"6#"' '); 9,o ma tran !8yen 5i ra man !in!> writeln('Ma tran ch%+en /i doc d%oc t% file la'); reset(f);readln(f"7);writeln(7); for i:= to n do for 6:= to n do Each if 6=n then begin readln(f"7);writeln(7);end else begin read(f"7);write(7"' ');end; while not eof(f) do begin while not eoln(f) do begin read(f"7); write(7"' '); end; readln(f); writeln; end; readln; end5

&?

Một số bài tập Pascal
{ 9hi bài 9+pe ban=record ten:string!:0#; sol%ong:integer; dongia:real; end; /ar s"st"s "s&"tena:string; sl"code:integer; dg"tong"1a7:real; f:te7t; a:arra+! 55 00# of ban; i"6"\$"n:integer; begin write('Nhap ten file:');readln(s); assign(f"s); rewrite(f);i:=0; repeat write('Nhap ten 1at hang :');readln(tena); if tena(*'' then begin i:=i) ; write(f"tena"' '); write('Nhap so l%ong:');readln(sl); write(f"sl"' '); write('Nhap don gia:');readln(dg); writeln(f"dg:&: ); 4writeln(f"dg)-; end; %ntil tena=''; close(f); reset(f); 6:= ; while not eof(f) do begin readln(f"st); 9,o 28 :ie8 t8 fi:e ra mang 1an g!i> a!6#5ten:=cop+(st" "pos(' '"st)' ); 9Ca ! 1> 4\$:=length(st); 4Ne% d%ng cach na+ thi writeln(f"dongia:&: )s :=''; while (st!\$#(*' ') do begin s :=st!\$#)s ;dec(\$); end; /al(s "a!6#5dongia"code); s&:=''; for n:=pos(' '"st)) to \$' do s&:=s&)st!n#; /al(s&"a!6#5sol%ong"code);9Ca !2> delete(st" "pos(' '"st)); 9Ne8 28ng a ! nay t!i Crite:n"f,2ongia.> /al(cop+(st" "pos(' '"st)' )"a!6#5sol%ong"code); delete(st" "pos(' '"st)); /al(st"a!6#5dongia"code);

&K

Một số bài tập Pascal
inc(6); end; close(f); for 6:= to i do with a!6# do writeln(ten"' '"sol%ong"' '"dongia:&: ); 9D8y 2in! 7o t!a; ;!an 1a7:=0;tong:=0; 6!i in ra ma !in!> for 6:= to i do with a!6# do begin tong:=tong)sol%ong8dongia; if 1a7( sol%ong8dongia then 1a7:=sol%ong8dongia; end; writeln('9ong th% nhap la'"tong:&: ); writeln('Aoai gao co tong th% nhap cao nhat la'); for 6:= to i do with a!6# do if sol%ong8dongia=1a7 then writeln(ten"' '); readln; end5 {Tim +iem nhi phan} t+pe 1ang=arra+! 55 00# of integer; /ar a:1ang;i"7"n:integer; 9,e <8y> f%nction ti1(a:1ang;da%"c%oi"7:integer):integer; /ar gi%a:integer; begin gi%a:=(da%)c%oi) di/ &; if a!gi%a#=7 then ti1:=gi%a else if 7(a!gi%a# then ti1:=ti1(a"da%"gi%a' "7) else ti1:=ti1(a"gi%a) "c%oi"7); end; 9=!ong 2e <8y> f%nction \$ie1(a:1ang;da%"c%oi"7:integer):integer; /ar gi%a:integer; \$t:boolean; begin \$t:=false; while (da%(=c%oi) and not(\$t)do begin gi%a:=(da%)c%oi) di/ &; if 7=a!gi%a# then begin \$ie1:=gi%a; \$t:=tr%e; end else if 7(a!gi%a# then c%oi:=gi%a' else da%:=gi%a) ; end; end; begin write('Nhap so phan t% c%a 1ang:');readln(n);

&N

Một số bài tập Pascal
for i:= to n do begin write('Nhap a!'"i"'#:');readln(a!i#); end; write('Nhap 7:');readln(7); writeln('@i tri c%a '"7"' la '"ti1(a" "n"7)); writeln('@i tri c%a '"7"' la '"\$ie1(a" "n"7)); readln; end5 /ar s:string;n"i"6:integer; a:arra+! 55 00# of string; {Xau dao} f%nction lap(s:string):string; /ar i:integer;st:string; begin st:=''; for i:=length(s) downto do st:=st)s!i#; lap:=st; end; f%nction de2%+(s:string):string; begin if s='' 4length(s)(= -then de2%+:=s else de2%+:=s!length(s)#)de2%+(cop+(s" "length(s)' )); end; {Xau doi xung} f%nction \$t(s:string):boolean; /ar i:integer;st:string; begin st:=''; for i:=length(s) downto do st:=st)s!i#; if st=s then \$t:=tr%e else \$t:=false; end; f%nction de2%+dao(s:string):boolean; begin if s='' 4length(s)( -then de2%+dao:=tr%e else if s! #(*s!length(s)# then de2%+dao:=false else begin delete(s" " ); delete(s"length(s)" ); de2%+dao:=de2%+dao(s); end; end; {Cap xep ten} proced%re swap(/ar 7"+:string);

&P

Một số bài tập Pascal
/ar tg:string; begin tg:=7; 7:=+; +:=tg; end; S%nction daoten(s:string):string; /ar st:string; begin st:='';s:=s)' '; while pos(' '"s)(* 0 do begin st:=cop+(s" "pos(' '"s)))st; delete(s" "pos(' '"s)); end; delete(st"length(st)" ); daoten:=st; end; begin write('Nhap 7a%:');readln(s); writeln('3a% dao la '"lap(s)); writeln('7a% dao la '"de2%+(s)); if \$t(s) then writeln('3a% doi 7%ng') else writeln('3a% \$hong doi 7%ng'); if de2%+dao(s) then writeln('3a% doi 7%ng') else writeln('3a% \$hong doi 7%ng'); write('Nhap so hoc sinh:');readln(n); for i:= to n do begin write('Nhap ho ten ng%oi th% '"i"':');readln(a!i#); end; for i:= to n' do for 6:=i) to n do if daoten(a!i#)*daoten(a!6#) then swap(a!6#"a!i#); writeln('>a+ sa% \$hi sap 7ep la'); for i:= to n do writeln(a!i#); readln; end5 {4han ! xau thiet lon} /ar . ".&:.9IJNG; f%nction cong(s "s&:string):string; /ar a"b"c"code"i"sod%:integer;ch"b "st:string; begin if length(s )*length(s&) then for i:= to length(s )'length(s&) do s&:='0')s&; if length(s&)*length(s ) then for i:= to length(s&)'length(s ) do s :='0')s ;st:='';

&O

Một số bài tập Pascal
for i:=length(s ) downto do begin /al(s !i#"a"code); /al(s&!i#"b"code); c:=(a)b)sod%)1od 0; sod%:=(sod%)a)b) di/ 0; str(c"ch); st:=ch)st; end; if sod%*0 then begin str(sod%"b ); st:=b )st; end; cong:=st; end; f%nction nhan(s:string;6:char):string; /ar \$2"ch"b :string;i"a"b"c"sod%"7"code:integer; begin sod%:=0; \$2:=''; /al(6"7"code); for i:=length(s) downto do begin /al(s!i#"a"code); c:=(a87)sod%) 1od 0; sod%:=(a87)sod%) di/ 0; str(c"ch); \$2:=ch)\$2; end; if sod%*0 then begin str(sod%"b ); \$2:=b )\$2; end; nhan:=\$2; end; f%nction nhanthiet(s "s&:string):string; /ar \$"tong:string; i"6"d:integer; begin tong:=nhan(s "s&!length(s&)#); \$:=''; for i:=length(s&)' downto do begin d:= ; for 6:= to d do \$:='0')\$; tong:=cong(tong"nhan(s "s&!i#))\$); inc(d);\$:=''; end; nhanthiet:=tong; end; <LGJN write('Nhap 7a% s :');readln(s );

:0

Một số bài tập Pascal
write('Nhap 7a% s&:');readln(s&); write('3a% \$et 2%a la '"nhanthiet(s "s&)); readln; LN>5 {<iet +e xau co n chu so trong do +hong co xau 010} /ar n:b+te; a:arra+! 55 00#of b+te; proced%re in\$2; /ar i:b+te; begin for i:= to n do write(a!i#); writeln; end; f%nction \$t:boolean; /ar i:b+te; begin for i:= to n'& do if (a!i#=0) and (a!i) #= ) and (a!i)&#=0) then \$t:=false end; proced%re tr+(6:b+te); /ar i:b+te; begin for i:=0 to do begin a!6#:=i; if (6=n) then begin if \$t then in\$2 end else tr+(6) ); end; end; begin write('Nhap n:');readln(n); tr+( ); readln; end5 {Dat +hoa thich hop theo de %ai} /ar a"st"s"s "s&:string; n"1"de1" i"6"d"\$:integer; f%nction \$t(s:string):boolean; /ar st:string; i:integer; begin st:=''; for i:=length(s) downto do st:=st)s!i#; if st=s then \$t:=tr%e else \$t:=false; end;

:

Một số bài tập Pascal
begin write('Nhap 7a% can ti1 \$hoa:');readln(st); s:=st)st; n:=&8length(st); i:= ;de1:=0;1:=0; while i(=n do <egin 6:=i) ; d:= ; while (6(=n) and (s!i#=s!6#) do begin inc(d); inc(6); end; \$:=6'd di/ &; a:=cop+(s" "\$' ); while (6(=n) and (s!6#(*s!i#) do inc(6); s :=cop+(s"\$"6'\$)d di/ &); s&:=cop+(s"6)d di/ &"length(s)))a; if (s =s&) and \$t(s ) then begin inc(de1);inc(1); writeln('@i tri dat \$hoa th% '"1"' la '"\$' ); writeln('@oi cach dat \$hoa nh% sa% '"s ); end; inc(i); end; if de1=0 then write(',hong co cach dat \$hoa') else writeln('.o cach dat la '"de1); {Tron mang theo pp chan o cuoi} 49+pe 1ang=arra+! 55 00# of integer; /ar a"b"c:1ang; i"1"\$"n"6:integer; begin write('Nhap n:');readln(n); for i:= to n do begin write('Nhap a!'"i"'#:');readln(a!i#); end; write('Nhap 1:');readln(1); for 6:= to 1 do begin write('Nhap b!'"6"'#:');readln(b!6#); end; a!n) #:=:&00 ; b!1) #:=:&00 ; i:= ;6:= ; for \$:= to 1)n do begin

:&

Một số bài tập Pascal
if a!i#(b!6# then begin c!\$#:=a!i#;inc(i);end else begin c!\$#:=b!6#;inc(6);end; end; writeln('>a+ sa% \$hi tron la'); for \$:= to 1)n do write(c!\$#);readln; end5 /ar f:te7t; s:string;ch:char; 7a"+a"7b"i"+b"7c"+c"de%"can :integer; d "d&"d::real; begin write('Nhap ten file:');readln(s); assign(f"s); rewrite(f); repeat write('Nhap 7a:');readln(7a); write(f"7a"' '); write('Nhap +a:');readln(+a); write(f"+a"' '); write('Nhap 7b:');readln(7b); write(f"7b"' '); write('Nhap +b:');readln(+b); write(f"+b"' '); write('Nhap 7c:');readln(7c); write(f"7c"' '); write('Nhap +c:');readln(+c); writeln(f"+c); write('<an co 1%on nhap tiep \$hong :(cX\$)');readln(ch); %ntil ch='\$'; reset(f);i:= ; while not eof(f) do begin readln(f"7a"+a"7b"+b"7c"+c); d :=s2rt(s2r(7b'7a))s2r(+b'+a)); d&:=s2rt(s2r(7c'7b))s2r(+c'+b)); d&:=s2rt(s2r(7c'7a))s2r(+c'+a)); if (d =d&) and (d&=d:) then begin writeln('9a1 giac th%'"i"' la ta1 gia de%'); inc(de%); end else if (d =d&) or (d =d:) or(d&=d:) then begin writeln('9a1 gia th% '"i"' la ta1 giac can'); inc(can); end; inc(i); end;

::

Một số bài tập Pascal
writeln('.o l%ong la1 gia de% la '"de%); writeln('so l%ong ta1 giac can la '"can); readln; end5

:=

Một số bài tập Pascal
{;ot so %ao toan "e ngay thang} 9Bn ngay t!ang nam tr8o ngay t!ang nam 2a !o> const nga+:arra+! 55 &#of integer=(: "&P": ":0": ":0": ": ":0": ":0": ); /ar nt"tt"ns"ts"d"1"+:integer; begin write('Nhap nga+:');readln(d); write('Nhap thang:');readln(1); write('Nhap na1');readln(+); if + 1od ==0 then nga+!&#:=&O; nt:=d' ; if nt=0 then begin tt:=1' ; if tt=0 then tt:= &; nt:=nga+!tt#; end else tt:=1; ns:=d) ; if ns*nga+!1# then begin ts:=1) ; if ts* & then ts:= ; ns:= ; end else ts:=1; writeln('Nga+ thang tr%oc la '"nt"' '"tt); writeln('Nga+ thang sa% la '"ns"' '"ts); 9 Bn ra nam am :i ! 8a mot nam 28ong :i !> const can:arra+!055O# of string =('Eanh'"'9an'"'Nha1'"'V%+'"'Giap'"'Ct'"'<inh'"'>inh'"'Ma%'"'\$+'); chi:arra+!055 # of string=('9han'"'>a%'"'9%at'"'Boi'"'9+' "'.%%'"'>an'"'Mao'"'9hin'"'9i'"'Ngo'"'M%i'); /ar n:integer; begin write('Nhap /ao na1:');readln(n); writeln('>o la na1 '"can!n 1od 0#"' '"chi!n 1od &#);9Bn ra m8a 8a mot t!ang> EFN.9 1%a:arra+! 55 &# of string=('3%an'"'3%an'"'3%an'"'Ba'"'Ba' "'Ba'"'9h%'"'9h%'"'9h%'"'>ong'"'>ong'"'>ong'); thang:arra+! 55 &# of string=('GJLNG'"'BCJ'"'<C'"'9D'"'NCM' "'.CD'"'<CM'"'9CM'"'EBJN'"'MDFJ'"'MDFJ MF9'"'MDFJ BCJ'); /ar t:string; i:integer; begin write('Nhap thang:');readln(t); for i:= to length(t) do t!i#:=%pcase(t!i#); for i:= to & do if thang!i#=t then write('9hang '"thang!i#"' la 1%a '"1%a!i#) readln; end5

:?

Một số bài tập Pascal