Đề cương lập trình Pascal

Màu nền
Font chữ
Font size
Chiều cao dòng

Bµi 1: NhËp sè tù nhiªn n vµ mét d•y sè thùc x1,x2,...,xn. 

-T×m sè lín nhÊt, nhá nhÊt cña d•y. 

-§Õm trong d•y cã bao nhiªu sè d­¬ng, bao nhiªu sè ©m, bao nhiªu sè 0? Lo¹i nµo nhiÒu nhÊt? 

Bµi gi¶i: 

Program BT1_dc; 

Uses CRT; 

Var A:array[1..50] of Real; 

i, j, n, Sd, Sa, So: Integer; 

Max, min:Real; 

BEGIN 

Clrscr; 

Write('Nhap so phan tu n= '); 

Readln(n); 

For i:=1 to n do 

Begin 

Write('A[ ', i, ' ]= '); 

Readln(A[i]); 

End; 

For i:=1 to n do write(A[i]:3:0); 

Max:=0; Min:=0; Sd:=0; Sa:=0; So:=0; 

For i:=1 to n do  

Begin 

If Max< A[i] then Max:=A[i]; 

If Min> A[i] then Min:=A[i]; 

End; 

Writeln('So lon nhat la: ',Max:8:2); 

Writeln('So nho nhat la: ', Min:8:2); 

For i:=1 to n do 

Begin 

If A[i]>0 then Sd:=Sd+1; 

If A[i]<0 then Sa:=Sa+1; 

If A[i]=0 then So:=So+1; 

End; 

Writeln('So duong co: ',Sd,' so'); 

Writeln('So am co: ',Sa,' so'); 

Writeln('So 0 xuat hien: ',So,' lan'); 

If (Sd>Sa) and (Sd>So) then writeln('So duong nhieu nhat') 

Else 

If (Sa>Sd) and (Sa>So) then writeln('So am nhieu nhat') 

Else 

If (So>Sd) and (So>Sa) then writeln('So 0 xuat hien nhieu nhat') 

Else  

writeln('Khong loai nao nhieu nhat'); 

Readln; 

END. 

Bµi 2: NhËp mét d•y sè nguyªn x1,x2,...,xn. In riªng c¸c sè ch½n, c¸c sè lÎ, mçi lo¹i trªn mét dßng. 

Bµi gi¶i: 

Program BT2_dc; 

Uses CRT; 

Var A:array[1..50] of Integer; 

i, n: Integer; 

BEGIN 

Clrscr; 

Write('Nhap so phan tu n= '); 

Readln(n); 

For i:=1 to n do 

Begin 

Writeln('Nhap A[ ',i,' ]= '); 

Readln(A[i]); 

End; 

Writeln('Cac phan tu la so chan gom: '); 

For i:=1 to n do 

If (A[i] mod 2 = 0) then write(A[i]:4); 

Writeln; 

Writeln('Cac phan tu la so le gom: '); 

For i:=1 to n do 

If ( A[i] mod 2 <>0) then write(A[i]:4); 

Writeln; 

Readln; 

END. 

Bµi 3: NhËp mét sè nguyªn d­¬ng n, x©y dùng d•y sè nguyªn x0, x1, x2, ... ,xn trong ®ã xi lµ sè Fibonaci thø i, x0=1, x1=1, xi= xi-1+ xi-2 víi mäi i > 2. In d•y x lªn mµn h×nh. 

Bµi gi¶i: 

Program BT3_dc; 

Uses CRT; 

Var F:array[0..100] of Integer; 

i, n: Integer; 

BEGIN 

Clrscr; 

Repeat 

Write('Nhap so phan tu n= '); 

Readln(n); 

Until (n >= 0); 

F[0]:=1; F[1]:=1; i:=2; 

While (i <= n) do 

Begin 

F[i]:=F[i-1] + F[i-2]; 

i:=i+1; 

End; 

For i:=1 to n do Write(F[i]); 

Readln; 

END. 

Bµi 4: NhËp mét d•y sè x1, x2, ..., xn. TÝnh: 

Bµi gi¶i: 

Program BT4_dc; 

Uses CRT; 

Var X: array [1..50] of integer; 

i, n: integer; 

A, B, S1, S2: real; 

BEGIN 

Clrscr; 

Write('Nhap so phan tu n= '); 

Readln(n); 

For i:=1 to n do 

Begin 

Write('X[ ',i,' ]= '); 

Readln(X[i]); 

End; 

A:=0;B:=0; 

For i:=1 to n do 

Begin 

A:=A+X[i]; 

B:=B+sqr(X[i]); 

i:=i+1; 

End; 

S1:=A*(1/n); 

Writeln('S1= ',S1); 

S2:= Sqrt(B); 

Writeln('S2= ',S2); 

Readln; 

END. 

Bai 5: NhËp mét d•y sè bÊt kú x1, x2,..., xn. 

-In ®¶o ng­îc d•y ®ã, vÝ dô cho d•y 1 5 3 8, in ra: 8 3 5 1 

-S¾p xÕp d•y t¨ng dÇn, in d•y 

-S¾p xÕp d•y gi¶m dÇn, in d•y 

Bµi gi¶i: 

Program Bai5_dc; 

Uses CRT; 

Var A: array[1..50] of real; 

i, j, n: integer; 

z: real; 

BEGIN 

Clrscr; 

Write('Nhap so phan tu n= '); 

Readln(n); 

For i:=1 to n do 

Begin 

Write('Nhap A[ ',I,' ]= '); 

Readln(A[i]); 

End; 

Writeln('Day dao nguoc la: '); 

For i:=n downto 1 do write(A[i]):3:0); 

Writeln; 

For i:=1 to n-1 do 

For j:=i+1 to n do  

If A[i]> A[j] then 

Begin 

Z:=A[i]; 

A[i]:= A[j]; 

A[j]:= z; 

End; 

Write('Day tang dan la: '); 

For i:=1 to n do write(A[i]:3:0); 

Writeln; 

For i:=1 to n-1 do 

For j:= i+1 to n do 

If A[i]< A[j] then 

Begin 

Z:= A[i]; 

A[i]:= A[j]; 

A[j]:= z; 

End; 

Writeln('Day giam dan la: '); 

For i:=1 to n do write(A[i];3;0); 

Readln; 

END. 

Bµi 6: NhËp hai d•y sè bÊt kú x1, x2,..., xn vµ y1, y2,..., yn. X©y dùng d•y thø ba z1, z2,..., zn lµ tæng cña hai d•y trªn (zi = xi + yi), in ba d•y lªn mµn h×nh, mçi d•y trªn mét dßng. 

Bµi gi¶i: 

Program Bai6_dc; 

Uses CRT; 

Var X, Y: array[1..50] of Integer; 

i, n, z: integer; 

BEGIN 

Clrscr; 

Write('Nhap so phan tu n= '); Readln(n); 

For i:=1 to n do 

Begin 

Write('Nhap A[ ',i,' ]= '); 

Readln(A[i]); 

End; 

For i:=1 to n do 

Begin 

Write('Nhap B[ ',i,' ]= '); 

Readln(B[i]); 

End; 

Write('Day X: '); 

For i:=1 to n do Write(X[i]:4); Writeln; 

Write('Day Y: '); 

For i:=1 to n Write(Y[i]:4); Writeln; 

Write('Day Z: '); 

For i:=1 to n do 

Begin 

Z := A[i]+B[i]; 

Write(Z:4); 

End; 

Readln; 

END. 

Bµi 7: NhËp mét d•y sè bÊt kú x1, x2, ...,xn. Cho biÕt d•y cã ®èi xøng kh«ng? D•y cã lËp thµnh 1 cÊp sè céng kh«ng? VÝ dô d•y 1 3 5 4 5 3 1 lµ ®èi xøng, d•y 1 3 5 7 lµ mét cÊp sè céng. 

Bµi gi¶i: 

Program BT7_dc ; 

Uses CRT ; 

Var X:array[1..100] of Integer ; 

I, d, n:Integer ; 

BEGIN 

Clrscr ; 

Write('Nhap so phan tu cua day n= ');Readln(n); 

For i :=1 to n do 

Begin 

Writeln('X[ ',i,' ]= '); 

Readln(X[i]) ; 

End ; 

{day co doi xung khong} 

i :=1; 

While(X[i]=X[n-i+1]) and (i<=n div 2) do i :=i+1 ; 

If (i>n div 2) then writeln('Day tren doi xung')  

Else writeln('Day tren khong doi xung') ; 

{Day co la cap so cong khong} 

I :=3 ; 

D :=X[2]-X[1] ; 

While(X[i]=X[i-1]+d) and (i<=n) do i :=i+1 ; 

If (i>n) then writeln('Day tren la cap so cong') 

Else writeln('Day tren khong la cap so cong') ; 

Readln ; 

END.  

Bµi 8: NhËp 1 d•y sè bÊt kú x1, x2, ..., xn. Cho biÕt d•y thuéc lo¹i nµo: t¨ng, gi¶m hay kh«ng t¨ng kh«ng gi¶m ? 

Bµi gi¶i: 

Program BT8_dc; 

Uses CRT; 

Var A:array[1..50] of Integer; 

I,j,n: integer; 

Kt:Boolean; 

BEGIN 

Clrscr; 

Write('Nhap so phan tu n= '); Readln(n); 

For i:=1 to n do 

Begin 

Writeln('A[ ',I,' ]= '); 

Readln(A[i]); 

End; 

Kt:=TRUE; 

For i:=1 to n-1 do 

For j:=i+1 to n do 

If A[i]< A[j] then kt:=TRUE 

Else  

If A[i]>A[j] then kt:=FALSE 

Else Writeln('Day khong tang khong giam'); 

If kt=TRUE then writeln('Day tang') 

Else writeln('Day giam'); 

Readln; 

END. 

Bµi 9: NhËp 1 d•y sè nguyªn d­¬ng x1, x2, ..., xn. T¸ch d•y x thµnh 2 d•y: D•y A gåm c¸c sè ch½n, d•y B gåm c¸c sè lÎ, s¾p xÕp d•y A t¨ng dÇn, d•y B gi¶m dÇn. In d•y A vµ d•y B trªn 2 dßng kh¸c nhau.  

Bµi gi¶i: 

Program BT9_dc ; 

Var X,A,B :array[1..100] of Integer ; 

I,p,q,n :Integer ; 

BEGIN 

Write('So phan tu n= ') ;Readln(n) ; 

P :=1 ;q :=1 ; 

For i :=1 to n do 

Begin 

Writeln('X[',i,']=') ; 

Readln(X[i]) ; 

End ; 

For i :=1 to n do 

If (X[i] mod 2=0) then 

Begin 

A[p] :=X[i] ; 

P :=p+1 ; 

End ; 

Else  

Begin 

B[q] :=X[i] ; 

Q :=q+1 ; 

End ; 

Writeln('Day ban dau :') ; 

For i :=1 to n do write(X[i] :4) ;Writeln ; 

Writeln('Day A :') ; 

For i :=1 to p-1 do write(A[i] :4) ;Writeln ; 

Writeln('Day B :') ; 

For i :=1 to q-1 do write(B[i] :4) ;  

Readln ; 

END. 

Bµi 10: Nèi 2 d•y A vµ B theo thø tù ®ã thµnh mét d•y duy nhÊt vµ g¸n trë l¹i vµo d•y x. In d•y x. VÝ dô: NhËp d•y x = {5, 7, 0, 2, 1, 6, 4, 9} th× d•y A = {0, 2, 4, 6}, d•y B = {9, 7, 5, 1} vµ x = {0, 2, 4, 6, 9, 7, 5, 1}. 

Bµi gi¶i: 

Program BT10_dc ; 

Var X,A,B :array[1..100] of Integer ; 

I,p,q,n :Integer ; 

BEGIN 

Write('So phan tu n= ') ;Readln(n) ; 

P :=1 ;q :=1 ; 

For i :=1 to n do 

Begin 

Writeln('X[',i,']=') ; 

Readln(X[i]) ; 

End ; 

For i :=1 to n do 

If (X[i] mod 2=0) then 

Begin 

A[p] :=X[i] ; 

P :=p+1 ; 

End ; 

Else  

Begin 

B[q] :=X[i] ; 

Q :=q+1 ; 

End ; 

Writeln('Day ban dau :') ; 

For i :=1 to n do write(X[i] :4) ;Writeln ; 

Writeln('Day A :') ; 

For i :=1 to p-1 do write(A[i] :4) ;Writeln ; 

Writeln('Day B :') ; 

For i :=1 to q-1 do write(B[i] :4) ; Writeln ; 

For i :=1 to p-1 do X[i] :=A[i] ; 

For i :=1 to q-1 do X[i+q-1] :=B[i] ; 

Writeln('Day X sau khi gan :') ; 

For i :=1 to n do write(X[i] :4) ; 

Readln ; 

END.

Bµi 11: S¾p xÕp d•y x1, x2, ..., xn sao cho c¸c sè d­¬ng ®øng tr­íc theo thø tù gi¶m dÇn råi ®Õn c¸c sè cßn l¹i ( sè ©m vµ sè 0) theo thø tù t¨ng dÇn. VÝ dô: NhËp d•y 3, 0, 4, -5, 2, -1, 7, 0, -6 s¾p thµnh: 7, 4, 3, 2, -6, -5, -1, 0, 0. 

Bµi gi¶i: 

Program BT11_dc; 

Var A:array[1..50] of Integer; 

U,z,I,j,n:Integer; 

BEGIN 

Write('Nhap so phan tu cua day n=');Readln(n); 

For i:=1 to n do 

Begin 

Writeln('A[',I,']='); 

Readln(A[i]); 

End; 

Writeln('Day duoc sap xep lai la:'); 

For i:=1 to n-1 do 

For j:=i+1 to n do 

If (A[i]<A[i]) and (A[i]>0) then 

Begin 

Z:=A[i]; 

A[i]:=A[j]; 

A[j]:=z; 

End; 

For i:=1 to n-1 do 

For j:=i+1 to n do 

If (A[i]>A[j]) and (A[i]<=0) then 

Begin 

U:=A[i]; 

A[i]:=A[j]; 

A[j]:=u; 

End; 

For i:=1 to n do write(A[i]); 

Readln; 

END. 

Bµi 12: NhËp chuçi St, x©y dùng chuçi St1 gåm c¸c ký tù cña St nh­ng ®¶o thø tù. §æi chuçi St thµnh ch÷ hoa vµ ®æi chuçi St1 thµnh ch÷ th­êng. VÝ dô cho St = 'AbcD12' th× St1 = '21DcbA', sau khi ®æi ta ®­îc St = 'ABCD12', St1 = '21dcba'. 

Bµi gi¶i: 

Program BT12_dc; 

Var St,St1:String[30]; 

I,x:Integer; 

BEGIN 

Write('Nhap chuoi');Readln(St); 

X:=length(St); 

St1:=' '; {1 dau cach} 

For i:=x downto 1 do St1:=St1+St[i]; 

Delete(St1,1,1); 

For i:=1 to x do St[i]:=Upcase(St[i]); 

For i:=1 to length(St1) do 

If (St1[i]>='A')and (St1[i]<='Z') then St1[i]:=Chr(Ord(St1[i])+32); 

Writeln('Chuoi St la: ',St); 

Writeln('Chuoi St1 la: ',St1); 

Readln; 

END. 

Bµi 13: NhËp chuçi St, xo¸ bá c¸c ký tù tr¾ng thõa ë ®Çu vµ ë cuèi sao cho ë gi÷a 2 tõ chØ cã ®óng 1 ký tù tr¾ng, ®æi chuçi thµnh ch÷ th­êng, riªng c¸c ch÷ ®Çu thµnh ch÷ hoa. 

Bµi gi¶i: 

Program BT13_dc; 

Var St:String[30]; 

I,k,h:Integer; 

BEGIN 

Write('Nhap chuoi St:');Readln(St); 

While St[1]=#32 do Delete(St,1,1); 

While St[length(St)]=#32 do Delete(St,length(St),1); 

K:=Pos(' ',St);{' ' co 2 dau cach} 

While k>0 do 

Begin 

Delete(St,k,1); 

K:=Pos(' ',St); 

End; 

For i:=1 to length(St) do 

If (St[i]>='A') and (St[i]<='Z') then St[i]:=Chr(Ord(St[i])+32); 

St[1]:= Upcase(St[1]); 

For i:=1 to length(St) do 

If (St[i]=' ' then St[i+1]:=Upcase(St[i+1]); 

Writeln('Chuoi St sau khi da duoc chinh sua la:',St); 

Readln; 

END.

Bµi 14: NhËp chuçi St, kiÓm tra chuçi cã ®èi xøng kh«ng? VÝ dô c¸c chuçi 'BCD1DCB' vµ 'ABCCBA' lµ ®èi xøng cßn 'ABCDBA' lµ kh«ng ®èi xøng. 

Bµi gi¶i: 

C1:Program BT14_dc; 

Var St,St1:String[30]; 

I,x: byte; 

BEGIN 

Write('Nhap chuoi St:');Readln(St); 

X:=length(St); 

St1:=' ';{1 dau cach} 

For i:=x downto 1 do St1:=St1+St[i]; 

If St=St1 then write('Chuoi tren la chuoi doi xung') 

Else write('Chuoi tren khong phai chuoi doi xung'); 

Readln; 

END. 

C2: Program BT14_dc; 

Var St:String[30]; 

I,n:Integer; 

Kt:Boolean; 

BEGIN 

Write('Nhap chuoi St:');Readln(St); 

Kt:=TRUE; 

For i:=1 to n do 

If St[i]<>St[n-i+1] then kt:=FALSE; 

If Kt=TRUE then write('Do la chuoi doi xung') 

Else write('Do khong phai chuoi doi xung'); 

Readln; 

END. 

Bµi 15: NhËp chuçi St, ®Õm xem trong chuçi cã bao nhiªu ch÷ a kh«ng ph©n biÖt viÕt hoa hay viÕt th­êng vµ cho biÕt vÞ trÝ c¸c ch÷ a ®ã. VÝ dô St = 'Anh van la quan trong' cã 4 ch÷ a t¹i vÞ trÝ 1, 6, 10, 14. 

Bµi gi¶i: 

Program BT15_dc; 

Var St:String[30]; 

I,dem:Integer; 

BEGIN 

Write('Nhap chuoi St');Readln(St); 

Dem:=0; 

Write('Chu a xuat hien tai cac vi tri:'); 

For i:=1 to length(St) do 

If (St[i]='a') or (St[i]='A') then 

Begin 

Dem:=dem+1; 

Write(I,', '); 

End; 

Writeln; 

Writeln('Nen so lan xuat hien la: ',dem); 

Readln; 

END. 

Bµi 16: NhËp 2 chuçi St vµ St1. Cho biÕt chuçi St1 xuÊt hiÖn mÊy lÇn trong chuçi St vµ t¹i c¸c vÞ trÝ nµo? VÝ dô St = 'pas12pas34', chuçi St1 = 'pas' xuÊt hiÖn 2 lÇn t¹i vÞ trÝ 1, 6. 

Bµi gi¶i: 

Program BT16_dc; 

Var St,St1:String[30]; 

I,dem: Integer; 

BEGIN 

Write('Nhap chuoi St:');Readln(St); 

Write('Nhap chuoi St1:');Readln(St1); 

Dem:=0; 

Write('St1 xuat hien trong chuoi St tai cac vi tri:'); 

For i:=1 to length(St)do 

Begin 

If St1=Copy(St,I,length(St1)) then 

Begin 

Dem:=dem+1; 

Write(I,', '); 

End; 

I:=i+1; 

End; 

Writeln; 

Write('Nen so lan xuat hien cua St1 trong St la:',dem); 

Readln; 

END. 

Bµi 17: NhËp 3 chuçi St, St1 vµ St2. T×m xem trong chuçi St cã chøa St1 kh«ng? NÕu cã th× thay thÕ St1 b»ng St2. VÝ dô St = 'ABC123E', St1 = '1234', St2 = '*'. Sau khi thay thÕ ta ®­îc St = 'ABC*E'. 

Bµi gi¶i: 

Program BT17_dc; 

Var St,St1,St2: String[30]; 

I: integer; 

BEGIN 

Write('Nhap chuoi St');Readln(St); 

Write('Nhap chuoi St1');Readln(St1); 

Write('Nhap chuoi St2');Readln(St2); 

For i:=1 to length(St) do 

Begin 

If St1=Copy(St,I,length(St1)) then  

Begin 

Delete(St,I,length(St1)); 

Insert(St2,St,i); 

End; 

I:=i+1; 

End; 

Writeln('Sau khi duoc thay the ta co chuoi St la:',St); 

Readln; 

End.  

C2:  

{...} 

K:=Pos(St1,St); 

While k>0 do 

Begin 

Delete(St,k,n); 

Insert(St2,St,l); 

K:=Pos(St1,St); 

End; 

{...} 

Bµi 18: ViÕt 1 hµm tÝnh k! víi k nguyªn d­¬ng bÊt kú. NhËp n, k tõ bµn phÝm (n = k = 0), sö dông hµm ®ã tÝnh sè tæ hîp chËp k cña n theo c«ng thøc: 

C  

Bµi gi¶i: 

Program BT18_dc; 

Var C,M:Real; 

N,k:Integer; 

{-------------------------------------------------} 

Function Gt(y:byte):real; {ham tinh Gt=y!} 

Var I:byte; 

Q:Real; 

Begin 

Q:=1; 

For i:=1 to y do Q:=Q*I; 

Gt:=Q; 

End; 

{-----------------------------------------------} 

BEGIN 

Repeat 

Write('Nhap n va k:'); 

Readln(n,k); 

Until (0<k) and(k<n); 

M:=Gt(k)*Gt(n-k); 

C:=Gt(n)/M; 

Writeln('To hop chap ',k,'cua ',n,' phan tu la: ',C:8:2); 

Readln; 

END.

Bµi 19: X©y dùng hµm Lt(x,k) ®Ó tÝnh luü thõa xk cña sè thùc x bÊt kú víi k nguyªn d­¬ng. NhËp sè x thùc vµ sè N nguyªn d­¬ng, sö dông hµm tÝnh Lt(x,k) tÝnh: 

S = 158-  

Bµi gi¶i: 

Program BT19_dc; 

Var S,x:Real; 

I,n: Integer; 

{-----------------------------------------------} 

Function Lt(y:Real;z:Byte):Real; {Ham tinh Lt=yz} 

Var j:Byte; 

Q:Real; 

Begin 

Q:=1; 

For j:=1 to z do Q:=Q*y; 

Lt:=Q; 

End; 

{-----------------------------------------------} 

BEGIN 

Repeat 

Write('Nhap n va x:'); 

Readln(n,x); 

Until (n>0); 

S:=Lt(15,8); 

For i:=1 to n do 

S:=S+Lt(-1,i)*Lt(x+I,i)/Lt(I,2); 

Writeln('S= ',S:10:2); 

Readln; 

END.

Bµi 20: Sö dông 2 hµm Lt(x,k) = xk vµ Gt(k) = k! tÝnh gÇn ®óng: (NhËp n nguyªn d­¬ng, x thùc) 

Bµi gi¶i: 

Program BT20_dc; 

Var S,x:Real; 

N,i:Integer; 

{----------------------------------------------------} 

Function Lt(x:Real;k:Byte):Real; {Ham tinh Lt=xk} 

Var j:Byte; 

Q:Real; 

Begin 

Q:=1; 

For j:=1 to k do Q:=Q*x; 

Lt:=Q; 

End; 

{----------------------------------------------------} 

Function Gt(k:Byte):Real; {Ham tinh Gt=k!} 

Var i:Byte; 

Q:=1; 

Begin 

Q:=1; 

For i:=1 to k Q:=Q*I; 

Gt:=Q; 

End; 

{----------------------------------------------------} 

BEGIN 

Repeat 

Write('Nhap n va x:'); 

Readln(n,x); 

Until (n>0); 

S:=1; 

For i:=1 to n do 

S:=S+ Lt(x,i)/Gt(i); 

Writeln('S= ',S:10:2); 

Readln; 

END. 

Bµi 21: NhiÖt ®é F (Fahrenheit) vµ nhiÖt ®é C (Cecius) liªn hÖ nhau theo c«ng thøc: 

C =  

ViÕt ch­¬ng tr×nh nhËp vµo 1 d•y c¸c ®é F1, F2, ..., Fn tuú ý, s¾p xÕp d•y nµy theo trËt tù t¨ng, ®èi víi mçi Fi ®ã h•y tÝnh vµ in lªn mµn h×nh c¸c ®é C t­¬ng øng, tr×nh bµy thµnh 2 cét: 

§é F §é C 

Yªu cÇu trong ch­¬ng tr×nh cã 2 hµm:  

-Hµm tÝnh ®é C theo ®é F. 

-Hµm tÝnh ®é F theo ®é C. 

Bµi gi¶i: 

Program BT21_dc; 

Uses CRT; 

Var C, F: array[1..100] of Real; 

n, i: Integer; 

{-------------------------------------------------------------------} 

Function f2c(k:Real):real; 

Begin 

f2c:= 5*(k-32)/9; 

End; 

{-------------------------------------------------------------------} 

Function c2f(k:Real):Real; 

Begin 

c2f:= 9*k/5+32; 

End; 

{-------------------------------------------------------------------} 

BEGIN 

Clrscr; 

Write('n= '); 

Readln(n); 

For i:=1 to n do 

Begin 

Write('F[ ',I, ' ]= '); 

Readln(F[i]); 

C[i]:= f2c(F[i]); 

End; 

Writeln('Bang nhiet do: '); 

Writeln('Do F Do C'); 

For i:=1 to n do 

Writeln(F[i]:4:1, C[i]:12:1); 

Readln; 

END.

Bµi 22: ViÕt 1 hµm t×m béi chung nhá nhÊt cña 2 sè nguyªn d­¬ng a vµ b bÊt kú. NhËp vµo n sè nguyªn d­¬ng A1, A2, ..., An. Dïng hµm nãi trªn t×m béi chung nhá nhÊt cña n sè ®ã. 

Bµi gi¶i: 

Program BT22_dc; 

Var A:array[1..50] of Integer; 

N,c,i:Integer; 

{----------------------------------------------------} 

Function BSCNN(a,b:Integer):Integer;{Ham tinh BCNN cua 2 so nguyen duong a va b} 

Var Max:Integer; 

Begin 

BSCNN:=0; 

If a>b then Max:=a else Max:=b; 

Repeat 

BSCNN:=BSCNN+Max; 

Until (BSCNN mod a=0) and (BSCNN mod b=0); 

End; 

{----------------------------------------------------} 

BEGIN 

Write('Nhap n= ');Readln(n); 

For i:=1 to n do 

Begin 

Write('A[',I,']='); 

Readln(A[i]); 

End; 

C:=BSCNN(A[1],A[2]); 

For i:=3 to n do C:=BSCNN(C,A[i]); 

Write('BCNN cua day la: ',C); 

Readln; 

END. 

Bµi 23: ViÕt 1 hµm ®Ó chuÈn ho¸ 1 chuçi: xo¸ bá mäi ký tù tr¾ng thõa ë ®Çu, cuèi chuçi vµ gi÷a 2 tõ chØ gi÷ l¹i ®óng 1 ký tù tr¾ng. 

Bµi gi¶i: 

Program BT23_dc; 

Var St:String[30] 

{----------------------------------------------------} 

Function chuanhoa(S:String):String; {Ham chuan hoa 1 chuoi ki tu} 

Var k,i:Integer; 

St1:String; 

Begin 

St1:=#32; 

While S[1]=#32 do Delete(S,1,1); 

While S[length(St)=#32 do Delete(S,length(S),1); 

K:=Pos(' ',St); {Co 2 dau cach} 

While k>0 do 

Begin 

Delete(S,k,1); 

K:= Pos(' ',St); 

St1:=St1+S; 

End; 

Chuanhoa:=St1; 

End; 

{----------------------------------------------------} 

BEGIN 

Write('Nhap chuoi St:');Readln(St); 

Writeln('Chuoi St da duoc chuan hoa la:',chuanhoa(St); 

Readln; 

END. 

Bµi 24: NhËp x thùc, n nguyªn d­¬ng, tÝnh gÇn ®óng cos(x): 

Cos(x)  

Bµi gi¶i: 

Program BT24_dc; 

Var S,x,S1,S2:Real; 

n,i,j,k:Integer; 

BEGIN 

Write('Nhap x=');Readln(x); 

Write('Nhap so nguyen duong n=');Readln(n); 

S:=1;S2:=1; 

For i:=1 to n do 

For j:=1 to 2*I do 

Begin 

S1:=Exp(2*i*ln(X)); 

S2:=S2*j; 

If (I mod 2=1) then k:=-1 else k:=1; 

S:=S+k*S1/S2; 

End; 

Writeln('Gia tri gan dung cua cosx la:',S:8:2); 

Readln; 

END. 

C2: Sd ham nhu bai 19 

{...} 

S:=1; 

For i:=1 to n do 

S:=S+Lt(-1,i)*Lt(x,2*i)/Gt(2*I); 

{...}

Bµi 25: Cho d•y Fibonaci x¸c ®Þnh nh­ sau: 

F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2 víi n = 2. 

H•y nhËp sè nguyªn d­¬ng n vµ tÝnh: S = F0 + F1 + F2 + ... +Fn. 

Bµi gi¶i: 

Program BT25_dc; 

Var n, I, F, F0, F1: Integer; 

S: LongInt; 

BEGIN 

Write('Nhap n= '); 

Readln(n); 

F0:= 0; 

F1:= 1; 

S:= 0; 

Writeln('Tong cua ', n+1, ' so Fibonaci dau tien la: '); 

For i:= 2 to n do 

Begin 

F:= F0+F1; 

F0:= F1; 

F1:= F; 

S:= S+F; 

End; 

Write(S:10:2); 

Readln; 

END. 

Bµi 26: NhËp sè nguyªn d­¬ng n. TÝnh S lµ tæng cña n sè nguyªn tè ®Çu tiªn. VÝ dô: n = 3 th× S = 2 + 3 + 5 = 10. 

Bµi gi¶i: 

Program BT26_dc; 

Var S,I,j,n:LongInt; 

{---------------------------------------------------} 

Function snt(k:LongInt):Boolean; {Ham kiem tra so nguyen to} 

Var i:longint;  

ok:boolean; 

Begin 

If (k<=1)or(k mod 2=0) then ok:=FALSE; 

If k>3 then 

if k mod 2=0 then ok:=FALSE 

else 

Begin 

i:=3; 

While i<=round(sqrt(k)) do 

Begin 

if k mod i=0 then ok:=FALSE; 

i:=i+2; 

End; 

End; 

snt:=ok; 

End; 

{---------------------------------------------------} 

BEGIN 

Write('n='); Readln(n); 

S:=0;i:=1;j:=2; 

Ok:=TRUE; 

While (i<=n) do 

Begin 

If snt(j) then 

Begin 

S:=S+j; 

I:=i+1; 

End; 

J:=j+1; 

End; 

Writeln('S= ',S); 

Readln; 

END. 

Bµi 27: NhËp 1 chuçi St, ®Õm xem trong St: 

-Cã bao nhiªu ch÷ c¸i A, B, C, ..., Z. 

-Cã bao nhiªu ch÷ sè 0, 1, 2, ..., 9. 

-Cã bao nhiªu ký tù tr¾ng. 

-Cã bao nhiªu ký tù kh¸c. 

Trong 4 lo¹i trªn th× lo¹i nµo nhiÒu nhÊt? 

Bµi gi¶i: 

Program BT27_dc; 

Uses CRT; 

Var St: String[30]; 

Dt, k, I, d1, d2, d3, d4: Integer; 

Phanloai: String; 

BEGIN 

Clrscr; 

Write('Nhap chuoi St: '); 

Readln(St); 

D1:= 0; d2:= 0; d3:= 0; d4:= 0; 

For i:= 1 to length(St) do 

Begin 

K:= Ord(A[i]); 

If k= 32 then d1:= d1+1 

Else if (k>= 48) and (k<=58) then d2:=d2+1 

Else if (k>=68) and (k<=123) then d3:= d3+1 

Else d4:= d4+1; 

End; 

D1:= max; 

If max < d2 then max:= d2; 

If max < d3 then max:= d3; 

If max < d4 then max:= d4; 

Writeln('Co ', d3, 'ki tu la cac chu cai'); 

Writeln('Co ', d2, 'ki tu la cac chu so'); 

Writeln('Co ', d1, 'ki tu trang'); 

Writeln('Co ', d4, 'ki tu khac'); 

Case dt of 

D1: phanloai:= 'ki tu trang'; 

D2: phanloai:= 'ki tu chu so'; 

D3: phanloai:= 'ki tu chu cai'; 

D4: phanloai:= 'ki tu khac'; 

End; 

Writeln('So ki tu nhieu nhat la: ', max, ' thuoc loai: ', phanloai); 

Readln; 

END. 

Bµi 28: NhËp 1 m¶ng gåm n tªn c¸c sinh viªn. H•y ®æi ra ch÷ hoa hÕt, s¾p xÕp vµ in lªn mµn h×nh theo thø tù a, b, c, ...VÝ dô nhËp 5 tªn: lan, an, anh, thanh, bich. In ra: AN, ANH, BICH, LAN, THANH. 

Bµi gi¶i: 

Program BT28_dc; 

Uses CRT;  

Var ds: array[1..100] of String[7]; 

i, n, j: Integer; 

tg: String[7]; 

{-----------------------------------------------------} 

Function chu_hoa(S:String):String; 

{Bien doi chu hoa thanh chu thuong} 

Var i,n: Integer; 

kq: String; 

Begin 

n:= length(S); 

kq:= ' '; {1 dau cach} 

For i:= 1 to n do kq:= kq + upcase(S[i]); 

chu_hoa:= kq; 

End; 

{-----------------------------------------------------} 

{Chuong trinh chinh} 

BEGIN 

Clrscr; 

Write('So nguoi la: '); Readln(n); 

For i:= 1 to n do 

Begin 

Write('Nguoi thu ', I, ' la: '); 

Readln(ds[i]); 

Ds[i]:= chu_hoa(ds[i]); 

End; 

For i:= 1 to n-1 do 

For j:= i+1 to n do 

If ds[i] > ds[j] then 

Begin 

tg:= ds[i]; 

ds[i]:= ds[j]; 

ds[j]:= tg; 

End; 

Writeln('Danh sach da xep la: '); 

For i:= 1 to n do writeln(i:4, ds[i]:10); 

Readln; 

END. 

Bµi 29: NhËp 1 sè nguyªn d­¬ng n, ®æi ra sè nhÞ ph©n (hÖ sè 2) t­¬ng øng. VÝ dô n = 15 ®æi ra thµnh 1111. 

Bµi gi¶i: 

Program BT29_dc; 

Uses CRT; 

Var A: array[1..50] of Integer; 

i, j, n: Integer; 

BEGIN 

Clrscr; 

Write('Nhap so nguyen duong n = '); Readln(n); 

i:= 1; 

Repeat 

A[i]:= n mod 2; 

n:= n div 2; 

i:= i+1; 

Until (n= 0); 

Write('Ket qua doi sang he nhi phan la: '); 

For j:= i-1 downto 1 do write(A[j]); 

Readln; 

END. 

Bµi 30: NhËp 1 chuçi St, sè nguyªn n vµ k. In ra mµn h×nh chuçi St2 lµ kÕt qu¶ cña chuçi St bÞ xo¸ ®i k ký tù kÓ tõ ký tù vÞ trÝ n. VÝ dô: St = 'ABCDEFGH' víi n = 2 vµ k = 4 th× kÕt qu¶ St2 = 'afgh'. (Kh«ng sö dông thñ tôc Delete trong lËp tr×nh Pascal).  

Bµi gi¶i: 

Program BT30_dc; 

Uses CRT; 

Var St, St2, St3, St4: String; 

k, n, m, t, s: Integer; 

BEGIN 

Clrscr; 

Write('Nhap chuoi St: ');Readln(St); 

Repeat 

Write('Nhap so nguyen n va k lan luot la: '); 

Readln(n,k) ; 

Until (n>0) and (k>0); 

s:= n+k; 

t:= n-1;  

m:= length(St); 

St3:= Copy(St,t,t); 

St4:= Copy(St,s,m-s+1); 

St2:= Concat(St3,St4); 

Writeln('Chuoi St2 la: ',St2); 

Readln; 

END.

Bạn đang đọc truyện trên: Truyen2U.Pro