Sie sind auf Seite 1von 44

# s 1

Bi 1.
Program bai1;
uses crt;
var
a,b,c,i:integer;
Begin
clrscr;
For i:=100 to 999 do
begin
a:=i div 100;
b:=(i div 10) mod 10;
c:=i mod 10;
if a*a*a+b*b*b+c*c*c=i then
begin
Writeln('So ',i,' co gia tri bang tong lap
phuong cac chu so cua no.');
end;
end;
End.

Bi 2.
Program bai2;
uses crt;
var
i:integer;
a,b,c,x,t:real;
Procedure tinh;
begin
a:=x;b:=0;t:=x;c:=1;
While abs(c*a/(2*b+1))>=0.0001 do
begin
a:=a*x;b:=b+1;c:=c*-1;
t:=t+c*a/(2*b+1);
end;
end;
Begin
clrscr;
tinh;
Writeln('Ket qua la:',t:0:10);

End.

Bi 3.
Program bai3;
uses crt;
var
a:array [1..1000] of real;
i,k,n:integer;
m:real;
Begin
clrscr;
Write('Ban hay nhap so phan tu cua day:');Readln(n);
For i:=1 to n do
begin
Write('Ban hay nhap so thu ',i,':');Readln(a[i]);
if (i>1) and (k<>3) then
begin
if (a[i]>a[i-1]) and (k<>1) then
if k=0 then k:=1 else k:=3;
if (a[i]<a[i-1]) and (k<>2) then
if k=0 then k:=2 else k:=3;
end;
end;
Case k of
1:Writeln('Day duoc sap xep theo thu tu tang dan.');
2:Writeln('Day duoc sap xep theo thu tu giam dan.');
3:begin
Writeln('Day chua duoc sap xep.');
Writeln('Sap xep lai theo thu tu tang dan:');
For k:=1 to n-1 do
For i:=1 to n do
if a[k]<a[i] then
begin
m:=a[i];a[i]:=a[k];a[k]:=m;
end;
For i:=1 to n do
Write(a[i]:0:2,' ');
end;
end;
End.

Bi 4.
Program bai4;

uses crt;
var
i,k,m,n:integer;
s:string;
Begin
clrscr;
Write('Ban hay nhap mot xau ki tu:');Readln(s);
While Pos(' ',s)<>0 do
Delete(s,Pos(' ',s),1);
Writeln('Xau da chuan hoa:',s);
End.

s 2
Bi 1.
Program bai1;
uses crt;
var
i,k,m,n:longint;
s:string;
Begin
clrscr;
Repeat
Write('Ban hay nhap so thu nhien n:');Readln(n);
if n<0 then Writeln('n phai la so tu nhien.');
Until n>=0;
Str(n,s);
Writeln('So ',n,' co ',length(s),' chu so.');
k:=0;m:=0;
For i:=1 to length(s) do
begin
k:=k+(ord(s[i])-48);
if i mod 2=1 then m:=m+(ord(s[i])-48)
else m:=m-(ord(s[i])-48);
end;
Writeln('Tong cac chu so cua ',n,' la:',k,'.');
Writeln('Chu so dau tien cua ',n,' la:',s[1],'.');
Writeln('Tong S la:',m);
Repeat
Write('Ban hay nhap so m(m<=so chu so cua
if m>length(s) then Writeln('m phai
<=',length(s),'.');
Until m<=length(s);
Delete(s,1,length(s)-m);
k:=0;
For i:=1 to length(s) do k:=k+(ord(s[i])-48);
Writeln('Tong ',m,' chu so cuoi cung cua ',n,'
la:',k,'.');
End.

Bi 2.
Program bai2;
uses crt;
var

## a:array [1..1000] of longint;

i,k,m,n:longint;
Begin
clrscr;
Repeat
Write('Ban hay nhap so phan tu cua day:');Readln(n);
if n<=0 then Write('Ban phai nhap so>0.');
Until n>0;
For i:=1 to n do
begin
Write('Ban hay nhap so thu ',i,':');Readln(a[i]);
end;
Writeln('Cac so khac nhau trong day la:');
For i:=1 to n do
begin
m:=0;
For k:=1 to i-1 do
if a[i]=a[k] then
begin m:=1;break;end;
if m=0 then Write(a[i],' ');
end;
End.

Bi 3.
Program bai3;
uses crt;
var
a:array [1..100,1..100] of integer;
i,k,m,n:longint;
Begin
clrscr;
Repeat
if n<=0 then Write('Ban phai nhap so>0.');
Until n>0;
For k:=1 to n do
For i:=1 to n do
begin
Write('Ban hay nhap phan tu
end;
For k:=1 to n do
5

begin
m:=1;
For i:=2 to n do
if abs(a[m,k])<abs(a[i,k]) then m:=i;
i:=a[m,k];a[m,k]:=a[k,k];a[k,k]:=i;
end;
clrscr;
For k:=1 to n do
For i:=1 to n do
begin
if i=k then Textcolor(14) else Textcolor(7);
Gotoxy(i*4,k);Write(a[i,k]);
end;
End.

Bi 4.
Program bai4;
uses crt,graph;
var
gd,gm,i,k:integer;
Begin
gd:=Detect;
Initgraph(gd,gm,'');
For k:=1 to 8 do
For i:=1 to 8 do
begin
if (i+k) mod 2=0 then SetFillStyle(1,15)
else SetFillStyle(1,8);
Bar(i*50+75,k*50,i*50+125,k*50+50);
end;
Setcolor(7);
Rectangle(125,50,525,450);
End.

s 3
Bi 1.
Program bai1;
uses crt;
var
a:array [1..1000] of longint;
kq:array [1..5] of integer;
i,k,n,d1,d2,d3:integer;
Begin
clrscr;
Repeat
Write('Ban hay nhap so phan tu cua day:');Readln(n);
if n<=0 then Writeln('Ban phai nhap so lon hon 0.');
Until n>0;
For i:=1 to n do
begin
Write('Ban hay nhap so thu ',i,':');Readln(a[i]);
if ((a[i]>a[kq[1]]) or (kq[1]=0)) and (a[i]<0)
then kq[1]:=i;
if ((a[i]<a[kq[2]]) or (kq[2]=0)) and (a[i]>0)
then kq[2]:=i;
if (a[i]>0) and (a[i-1]>0) and (i>1) then
begin if d1=0 then d1:=2 else d1:=d1+1;end
else
begin if kq[3]<d1 then kq[3]:=d1;d1:=0;end;
if (a[i]<0) and (a[i-1]<0) and (i>1) then
begin if d2=0 then d2:=2 else d2:=d2+1;end
else
begin if kq[4]<d2 then kq[4]:=d2;d2:=0;end;
if (a[i]*a[i-1]<0) and (i>1) then
begin if d3=0 then d3:=2 else d3:=d3+1;end
else
begin if kq[5]<d3 then
kq[5]:=d3;kq[5]:=d3;d3:=0;end;
end;
if kq[3]<d1 then kq[3]:=d1;
if kq[4]<d2 then kq[4]:=d2;
if kq[5]<d3 then kq[5]:=d3;
if kq[1]<>0 then Writeln('So hang am lon nhat la so
thu ',kq[1],':',a[kq[1]],'.')
else Writeln('Khong co so am nao trong day.');
if kq[2]<>0 then Writeln('So hang duong nho nhat la so
thu ',kq[2],':',a[kq[2]],'.')
else Writeln('Khong co so duong nao trong day.');

## if kq[3]<>0 then Writeln('So luong cac so duong lien

tiep lon nhat la:',kq[3],'.')
else Writeln('Khong co cac so duong nao lien tiep
trong day.');
if kq[4]<>0 then Writeln('So luong cac so am lien tiep
lon nhat la:',kq[4],'.')
else Writeln('Khong co cac so am nao lien tiep trong
day.');
if kq[5]<>0 then Writeln('So luong cac so dan dau lien
tiep lon nhat la:',kq[5],'.')
else Writeln('Khong co cac so dan dau nhau nao lien
tiep trong day.');
End.

Bi 2.
Program bai2;
uses crt;
var
a:array [1..100,1..100] of integer;
i,k,m,n,c1,c2,c3,c4:integer;
Begin
clrscr;
Repeat
if n<=0 then Writeln('Ban phai nhap so lon hon 0.');
Until n>0;
clrscr;
m:=0;
c1:=1;c2:=1;c3:=n;c4:=n;
Repeat
For i:=c1 to c3 do
begin
m:=m+1;
a[i,c2]:=m;
Gotoxy(i*3,c2);Write(a[i,c2]);
end;
c2:=c2+1;
For i:=c2 to c4 do
begin
m:=m+1;
a[c3,i]:=m;
Gotoxy(c3*3,i);Write(a[c3,i]);
end;
c3:=c3-1;
For i:=c3 downto c1 do

begin
m:=m+1;
a[i,c4]:=m;
Gotoxy(i*3,c4);Write(a[i,c4]);
end;
c4:=c4-1;
For i:=c4 downto c2 do
begin
m:=m+1;
a[c1,i]:=m;
Gotoxy(c1*3,i);Write(a[c1,i]);
end;
c1:=c1+1;
Until m>=n*n;
End.

Bi 3.
Program bai3;
uses crt;
var
i,k,m,n:integer;
s:string;
Begin
clrscr;
Write('Ban hay nhap mot xau ki tu:');Readln(s);
While Pos(' ',s)<>0 do
Delete(s,Pos(' ',s),1);
Writeln('Xau da chuan hoa:',s);
End.

Bi 4.
Program bai4;
uses crt;
var
i:integer;
a,b,x,t:real;
Procedure tinh;
begin
i:=1;
a:=x-1;b:=x+1;t:=a/(i*b);
While a/(i*b)>=0.0001 do
begin

i:=i+2;
a:=a*(x-1)*(x-1);
b:=b*(x+1)*(x+1);
t:=t+a/(i*b);
end;
end;
Begin
clrscr;
if x<>-1 then
begin
if x=1 then Writeln('Ket qua la:0')
else
begin
tinh;
Writeln('Ket qua la:',t:0:10);
end;
end
else Writeln('Cac phan tu co mau la 0 nen the chia
duoc.');
End.

1
0

s 4
Bi 1.
Lnh Format a mm vi cc tham s:
/S: Sau khi nh dng xong th copy ln a cc tp tin
h thng v c th dng a ny khi ng my.
/U: Khng th khi phc li cc thng tin trn a ny.
/Q: nh dng nhanh 1 a cha thng tin.
Nu nh 1 a cha thng tin th sau khi nh dng xong
th thng tin trn a s b mt.

Bi 2.
Program bai2;
uses crt;
var n,i:integer;
tong:real;
t:char;
{Chuong trinh chinh}
begin
repeat
clrscr;
writeln('
CHUONG TRINH TINH TONG N SO');
writeln('S = 1 + 1/2 + 1/3 +...+ 1/n');
writeln;
write('Nhap so gioi han can tinh, n=');readln(n);
{Tinh tong}
tong:=1;
for i:=2 to n do tong:=tong+1/i;
writeln('Ket qua, S =',tong:7:4);
writeln;
writeln('An Phim BAT KY de TIEP TUC, X de
THOAT!...');
until (t='x') or(t='X');
end.

Bi 3.
program bai3;
uses crt;
var i,k:integer;
{=======================================================
}

1
1

## Ham Kiem tra 1 so nguyen xem so do co phai la so

nguyen to khong?
Neu la so nguen to tra ve gia tri la 1
Nguoc lai tra ve gia tri }
function snt(var a:integer):integer;
var i,d:integer;
begin
d:=0;
for i:=2 to round(sqrt(a)) do
if (a mod i)=0 then d:=d+1;
if d=0 then snt:=1
else snt:=0;
end;
{*******************************************************
****}
{Chuong trinh chinh}
begin
clrscr;
writeln('CHUONG TRINH IN CAC SO NGUYEN TO TU 1 DEN
100');
writeln;
k:=0;
for i:=1 to 100 do
if snt(i)<>0 then
begin
write(i:5);
k:=k+1;
if k=13 then
begin
writeln;
k:=0;
end;
end;
writeln;
writeln('An Phim Bat ky de THOAT!...');
repeat until keypressed;
end.

Bi 4.
program bai4;
uses crt;
type m = array [1..20,1..20] of integer;
var a,b:m;
h,c:integer;
{=================================================}

1
2

## procedure in1(var b:m;u:integer;v:integer);

var i,j:integer;
tg:integer;
begin
for j:=1 to v do b[1,j]:=j;
for i:=1 to u do b[i,1]:=i;
{}
for i:=2 to u do
for j:=2 to v do
begin
tg:=i*b[1,j];
b[i,j]:=tg;
end;
end;
{=================================================}
procedure in2(var b:m;u:integer;v:integer);
var i,j:integer;
begin
for j:=1 to v do b[1,j]:=j;
for i:=1 to u do b[i,1]:=i;
{}
for j:=2 to v do
for i:=2 to u do
b[i,j]:=b[i-1,j]+j;
end;
{=================================================}
procedure hienthi(var b:m;u:integer;v:integer);
var i,j:integer;
begin
for i:=1 to u do
begin
for j:=1 to v do
write(b[i,j]:5);
writeln;
end;
end;
{=================================================}
begin
clrscr;
h:=9;
c:=9;
writeln('
IN BANG CUU CHUONG');
in1(a,h,c);
writeln('Cach 1:');
hienthi(a,h,c);
writeln;
writeln('Cach 2:');
in2(a,h,c);
hienthi(a,h,c);
writeln;
write('An Phim Bat ky de THOAT!...');

1
3

## repeat until keypressed;

end.

1
4

s 5
Bi 1.
Program bai1;
uses crt;
var
i:integer;
x:real;
Function tinh(i:integer):real;
var
r:real;
begin
if i<256 then r:=x*x+i/tinh(i*2)
else r:=x*x+i/(x*x);
tinh:=r;
end;
Begin
clrscr;
Repeat
Write('Ban hay nhap so thuc x:');Readln(x);
if x=0 then Writeln('Ban phai nhap x khac 0.');
Until x<>0;
Write('Ket qua la:',tinh(2):0:2);
End.

Bi 2.
Program bai2;
uses crt;
var
a:array [1..1000,1..2] of longint;
i,k,m,n,t,p:longint;
ck:string;
Function ucln(m,n:longint):longint;
begin
While m<>n do
begin
if m<n then n:=n-m;
if m>n then m:=m-n;
end;
ucln:=m;
end;
Procedure tinh(m,n:longint);
begin

1
5

## While n>m do n:=n-m;

k:=ucln(m,n);n:=n div k;m:=m div k;
i:=0;t:=0;
n:=n*10;
Repeat
i:=i+1;
a[i,1]:=n div m;
a[i,2]:=n;
if n mod m=0 then t:=2;
n:=(n mod m)*10;
For k:=1 to i-1 do
if (a[k,1]=a[i,1]) and (a[k,2]=a[i,2]) then
begin
p:=k;t:=1;Break;
end;
Until (t<>0) or (i>1000);
if t=1 then For k:=t to i-1 do
ck:=ck+chr(a[k,1]+48)
else ck:='0';
end;
Begin
clrscr;
Repeat
Write('Ban hay nhap so tu nhien n:');Readln(n);
Write('Ban hay nhap so tu nhien m:');Readln(m);
if n*m<0 then Writeln('Ban phai nhap 2 so tu
nhien.');
if m=0 then Writeln('m phai khac 0.');
Until (n*m>=0) and (m<>0);
if n mod m=0 then ck:='0'
else tinh(m,n);
Writeln(n,'/',m,' co chu ky = ',ck);
End.

Bi 3.
Program bai3;
uses crt;
var
a:array [1..1000] of longint;
kq:array [1..3] of integer;
i,n:integer;
Begin
clrscr;
Repeat

1
6

## Write('Ban hay nhap so phan tu cua day:');Readln(n);

if n<=0 then Writeln('Ban phai nhap so lon hon 0.');
Until n>0;
For i:=1 to n do
begin
Write('Ban hay nhap so thu ',i,':');Readln(a[i]);
if i>1 then
begin
if (a[i]>0) and (a[i-1]>0) then
kq[1]:=kq[1]+1;
if (a[i]*a[i-1]>=0) and (abs(a[i1])>abs(a[i])) then kq[2]:=kq[2]+1;
if a[i]*a[i-1]<0 then kq[3]:=kq[3]+1;
end;
end;
Writeln('Co ',kq[1],' cap phan tu ke nhau ma ca hai so
deu duong.');
Writeln('Co ',kq[2],' cap phan tu ke nhau ma ca hai so
cung dau va so dung truoc co gia tri tuyet doi lon hon
so dung sau.');
Writeln('Co ',kq[3],' cap phan tu ke nhau ma ca hai so
trai dau nhau.');
End.

Bi 4.
Program bai4;
uses crt;
var
i,k:integer;
s1,s2,s3:string;
f:Text;
Begin
clrscr;
Assign(f,'vanban.pas');
Reset(f);
Close(f);
Writeln('Ba xau ki tu ban dau:');
Writeln('S1=',s1);
Writeln('S2=',s2);
Writeln('S3=',s3);
For i:=1 to length(s1) do
begin

1
7

k:=Pos(s1[i],s2);
if (k>0) and (k<length(s3)) then
begin
Writeln('(',s1[i],',',i,',',k,',',s3[k],')');
s1[i]:=s3[k];
end;
end;
Writeln('Xau S1 sau khi bien doi:');
Writeln(s1);
End.

1
8

s 6
Bi 1. Tn s xut hin ch ci
Program bai1;
uses crt;
var
a:array [1..25] of integer;
i:integer;
c:char;
f:Text;
Begin
clrscr;
Assign(f,'vanban.pas');
Reset(f);
While not eof(f) do
begin
if Upcase(c) in ['A'..'Z'] then
a[ord(Upcase(c))-64]:=a[ord(Upcase(c))-64]+1;
end;
Close(f);
Assign(f,'kqvanban.pas');
Rewrite(f);
For i:=1 to 25 do
if a[i]>0 then
Writeln(f,'Chu cai ',chr(96+i),' xuat hien ',a[i],'
lan.');
Close(f);
End.

## Bi 2. Tam gic Pascal

Program bai2;
uses crt;
var
a:array [1..100,1..100] of longint;
i,j,k,m,n,p:longint;
c:char;
Procedure nhap(w:string;var a:longint);
var
k:integer;
s:string;
begin
Repeat
Val(s,a,k);
Until k=0;

1
9

end;
Begin
Repeat
clrscr;
Repeat
k:=0;
nhap('Ban hay nhap so n:',n);
if n<=0 then k:=1;
if n>80 then
begin
Write('Ban co chac chan muon nhap so n lon nay
khong?(c/k)');
Repeat
Until Upcase(c) in ['C','K'];
if Upcase(c)='K' then k:=1;
Writeln;
end;
Until k=0;
clrscr;
Writeln('n=',n);
For i:=1 to n do a[1,i]:=1;
Gotoxy(40,2);Write('1');
For k:=2 to n do
For i:=1 to k do
begin
if i>1 then a[i,k]:=a[i-1,k-1]+a[i,k-1];
Gotoxy(37-k*6 div 2+i*6,k+1);Write(a[i,k]);
end;
Writeln;
if i>14 then
begin
Textcolor(4);
Writeln('Ban nhap so n hoi lon so voi be rong man
hinh cho phep.');
Textcolor(7);
end
else Writeln;
Repeat
Repeat
nhap('Ban hay nhap so i(hang i):',i);
if (i>n) or (i<1) then
Writeln('So i phai nam trong khoang tu 1>n(0<i<',n+1,')');
Until i in [1..n];
nhap('Ban hay nhap so j:',j);
p:=0;
For m:=1 to i do
if a[m,i]=j then p:=p+1;
if p=0 then
Writeln('Hang ',i,' khong co so ',j,' nao.')

2
0

else
Writeln('Hang ',i,' co ',p,' so ',j,'.');
Write('Ban co muon nhap tiep hai so i va j khong?
(c/k)');
Writeln;
Until Upcase(c)='K';
Write('Ban co muon thuc hien lai ca chuong trinh khong?
(c/k)');
Until Upcase(c)='K';
End.

Bi 3. Xu nghch o
Program bai3;
uses crt;
var
i,k:integer;
s:string;
Procedure dao(i:integer);
begin
if i<=length(s) then
begin
if s[i]='0' then s[i]:='1'
else s[i]:='0';
dao(i+1);
end;
end;
Begin
clrscr;
Repeat
Write('Ban hay nhap 1 xau nhi phan:');Readln(s);
k:=0;
For i:=1 to length(s) do
if not (s[i] in ['0','1']) then
begin
k:=1;Break;
end;
Until k=0;
dao(1);
Writeln('Xau nghich dao:',s);
End.

2
1

Bi 4. S hon ho
Program bai4;
uses crt;
var
i,k,m,n,s:integer;
Begin
clrscr;
Repeat
Until n>=0;
For i:=2 to n do
if i mod 2=0 then
begin
m:=1;
For k:=2 to i-1 do
if i mod k=0 then m:=m+k;
if m=i then
begin
s:=s+1;
Writeln(i);
end;
end;
Writeln('Co ',s,' so hoan hao co gia tri tu 1 den ',n,'.');
End.

2
2

s 7
Bi 1. Bi ton s nguyn t tng ng
Program Bai1;
uses crt;
var
a:array [1..100] of integer;
i,k,m,n:integer;
Begin
clrscr;
Write('Ban hay nhap so thu nhat:');Readln(m);
Write('Ban hay nhap so thu hai:');Readln(n);
if m<n then begin k:=m;m:=n;n:=k;end;
i:=1;k:=0;
Repeat
i:=i+1;
if (m mod i=0) and (n mod i=0) then
begin
if k=0 then
Writeln('Hai so tren la hai so nguyen to tuong
duong voi nhau vi co cac uoc nguyen to:');
k:=1;Write(i,' ');
While (m mod i=0) and (n mod i=0) do
begin
m:=m div i;n:=n div i;
end;
end;
Until i>=n;
if k=0 then
Writeln('Hai so tren khong phai la hai so nguyen to tuong
duong voi nhau.');
End.

Bi 2. S siu nguyn t
Program Bai2;
uses crt;
const
d:array [1..6] of char=('1','2','3','5','7','9');
var
i,k,m,n:integer;
s:string;
c:char;
Function kt(s:string):boolean;
var
k:integer;
a,i:real;

2
3

b:boolean;
begin
Val(s,a,k);
b:=True;
i:=1;
While i<=sqrt(a) do
begin
i:=i+1;
if a/i=round(a/i) then begin b:=False;Break;end;
end;
kt:=b;
end;
Procedure tim(i:integer);
var
k:integer;
begin
For k:=1 to 6 do
if (i=1) or ((i>1) and (k<>2) and kt(copy(s,1,i1)+d[k])) then
begin
s[i]:=d[k];
if i=n then
begin
Writeln(s);
m:=m+1;
end
else tim(i+1);
end;
end;
Begin
clrscr;
s:='';
For i:=1 to n do s:=s+'0';
tim(1);
Writeln('Co ',m,' so.');
End.

Bi 3. Tm im yn nga.
Program Bai3;
uses crt;
var
a:array [1..100,1..100] of integer;
h:array [1..100,1..2] of integer;
c:array [1..100,1..2] of integer;
kq:array [1..2,1..100] of integer;
i,k,m,n,s:integer;
Procedure tim;
var

2
4

i,k,z,p:integer;
begin
For i:=1 to n do
begin
h[i,1]:=1;h[i,2]:=1;
For k:=2 to m do
begin
if a[k,i]<a[h[i,1],i] then h[i,1]:=k;
if a[k,i]>a[h[i,2],i] then h[i,2]:=k;
end;
end;
For i:=1 to m do
begin
c[i,1]:=1;c[i,2]:=1;
For k:=2 to n do
begin
if a[i,k]<a[i,c[i,1]] then c[i,1]:=k;
if a[i,k]>a[i,c[i,2]] then c[i,2]:=k;
end;
end;
end;
Procedure tinh;
begin
For i:=1 to n do
begin
if c[h[i,1],2]=i then
begin
s:=s+1;kq[s,1]:=h[i,1];kq[s,2]:=i;
end;
if c[h[i,2],1]=i then
begin
s:=s+1;kq[s,1]:=h[i,2];kq[s,2]:=i;
end;
end;
end;
Begin
clrscr;
For k:=1 to n do
For i:=1 to m do
begin
Write('Ban hay nhap phan tu
end;
clrscr;
For k:=1 to n do
For i:=1 to m do
begin
Gotoxy(i*4,k);Write(a[i,k]);
end;

2
5

tim;
tinh;
Writeln;
For i:=1 to s do
Writeln('Phan tu
A[',kq[i,1],',',kq[i,2],']=',a[kq[i,1],kq[i,2]],' la phan tu
yen ngua.');
End.

Bi 4. Bi ton thay th t.
Program Bai4;
uses crt;
var
a:array [1..100] of string;
i,k,m,n:integer;
s,s1,s2:string;
f:text;
Procedure sua;
begin
While s[1]=' ' do Delete(s,1,1);
s1:='';While s[1]<>' ' do begin
s1:=s1+s[1];Delete(s,1,1);end;
While Pos(' ',s)>0 do Delete(s,Pos(' ',s),1);s2:=s;
For i:=1 to n do
begin
if Pos(s1,a[i])>0 then
begin
k:=Pos(s1,a[i]);
if (not (a[i,k-1] in
['a'..'z','A'..'Z','0'..'9']))
and (not (a[i,k+length(s1)] in
['a'..'z','A'..'Z','0'..'9'])) then
begin
Delete(a[i],k,length(s1));
Insert(s2,a[i],k);
end;
end;
end;
end;
Begin
clrscr;
Assign(f,'dlieu1.txt');
Reset(f);
While not eof(f) do
begin
end;
Close(f);
Assign(f,'dlieu2.txt');

2
6

Reset(f);
While not eof(f) do
begin
end;
Close(f);
Assign(f,'ketqua.txt');
Rewrite(f);
For i:=1 to n do
Writeln(f,a[i]);
Close(f);
End.

2
7

s 8
Bi 1. Tr chi
Program game;
var
a:array [1..1000] of integer;
i,k,m,n:integer;
f:Text;
Procedure doc;
begin
Assign(f,'game.inp');
Reset(f);
For i:=1 to n do Readln(f,a[i]);
Close(f);
end;
Procedure tinh;
begin
k:=0;
if m mod 2=0 then
begin
For i:=1 to n do
if i mod 2=1 then k:=k+a[i];
end
else
For i:=1 to n do
if i mod 2=0 then k:=k+a[i];
end;
Procedure ghi;
begin
Assign(f,'game.out');
Rewrite(f);
Writeln(f,k);
Close(f);
end;
Begin
doc;
tinh;
ghi;
End.

Bi 2. Xp khch
Program room;
var
a,b:array [1..1000] of integer;
i,k,m,n,p,q:integer;
f:Text;

2
8

Procedure doc;
begin
Assign(f,'room.inp');
Reset(f);
For i:=1 to m do
Close(f);
end;
Procedure tinh;
begin
For i:=1 to m do
begin
p:=a[i];k:=0;
Repeat
k:=k+1;
if b[k]=0 then
if p>1 then begin b[k]:=2;p:=p-2;end
else begin b[k]:=1;p:=0;end;
Until (k>=n) or (p=0);
if p>0 then
begin
k:=0;
Repeat
k:=k+1;
if b[k]=1 then begin b[k]:=2;p:=p-1;end;
Until (k>=n) or (p=0);
end;
end;
end;
Procedure ghi;
begin
Assign(f,'room.out');
Rewrite(f);
For i:=1 to n do
Writeln(f,b[i]);
Close(f);
end;
Begin
doc;
tinh;
ghi;
End.

Bi 3. Hnh vung
Program square;
var
a:array [1..100,1..100] of integer;
b,kq:array [1..1000,1..2] of integer;
i,k,m,n,p,q,z,s,l:integer;

2
9

c:char;
f:Text;
Procedure kt(x1,y1,x2,y2:integer);
var
i,k,t,p,q:integer;
begin
t:=0;
i:=0;
k:=x2-x1;
Repeat
i:=i+1;
if a[x1+i,y1]=0 then t:=1;
if a[x1+i,y2]=0 then t:=1;
if a[x1,y1+i]=0 then t:=1;
if a[x2,y1+i]=0 then t:=1;
Until (i=k) or (t=1);
if t=0 then
begin
q:=0;
For p:=1 to l do
if kq[p,2]=k div 2 then begin
kq[p,1]:=kq[p,1]+1;q:=1;Break;end;
if q=0 then
begin
l:=l+1;kq[l,1]:=1;kq[l,2]:=k div 2;
end;
end;
end;
Procedure doc;
begin
Assign(f,'square.inp');
Reset(f);
For i:=1 to k do
begin
Repeat
Until Upcase(c) in ['H','V'];
if n<q then n:=q;
a[p*2-1,q*2-1]:=1;
if c='H' then
begin
a[p*2,q*2-1]:=1;a[p*2+1,q*2-1]:=1;
end
else
begin
a[p*2-1,q*2]:=1;a[p*2-1,q*2+1]:=1;
end;
end;

3
0

Close(f);
end;
Procedure tinh;
begin
For i:=1 to n-1 do
For k:=1 to m-1 do
if a[i*2-1,k*2-1]=1 then
For p:=1 to m-i do
kt(i*2-1,k*2-1,i*2-1+p*2,k*2-1+p*2);
end;
Procedure ghi;
begin
Assign(f,'square.out');
Rewrite(f);
Writeln(f,l);
For i:=1 to l do
Writeln(f,kq[i,1],' ',kq[i,2]);
Close(f);
end;
Begin
doc;
tinh;
ghi;
End.

3
1

s 9
Bi 1.
Program bai1;
uses crt;
var
i,k,m,n,nd,td,yd,nc,tc,yc:integer;
Function ng(t,n:integer):integer;
var
a:integer;
begin
Case t of
4,6,9,11:a:=30;
1,3,5,7,8,10,12:a:=31;
2:if n mod 4<>0 then a:=28 else a:=29;
end;
ng:=a;
end;
Begin
clrscr;
Writeln('Ban hay nhap ngay thang nam bat dau:');
Writeln('Ban hay nhap ngay thang nam cuoi:');
n:=0;
if yd<>yc then
begin
For i:=yd+1 to yc-1 do
if i mod 4<>0 then n:=n+365 else n:=n+366;
For i:=td to 12 do
n:=n+ng(i,yd);
n:=n-nd;
For i:=1 to tc-1 do
n:=n+ng(i,yc);
n:=n+nc;
end;
Writeln('Giua hai moc thoi gian do co ',n,' ngay.');
End.

Bi 2.
Program Bai2;
uses crt;

3
2

var
i,k,n:integer;
Begin
clrscr;
For k:=1 to n do
begin
For i:=1 to n do
if (i in [k,k+1]) or ((k=n) and (i=1)) then
Write('*')
else Write('-');
Writeln;
end;
End.

Bi 3.
Program bai3;
uses crt;
var
a:array [1..1000] of integer;
i,j,k,m,n,ch,p:integer;
c:char;
Procedure nhap;
begin
Write('Ban hay nhap so phan tu:');Readln(n);
k:=0;
ch:=0;
For i:=1 to n do
begin
if (i>1) and (k=0) then
begin
if a[i]>a[i-1] then
begin
if ch=0 then ch:=1;
if ch=2 then k:=1;
end;
if a[i]<a[i-1] then
begin
if ch=0 then ch:=2;
if ch=1 then k:=1;
end;
end;
end;
if k=0 then
begin
Case ch of
0,1:Writeln('Day duoc sap xep theo thu tu tang dan.');

3
3

## 2:Writeln('Day duoc sap xep theo thu tu giam dan.');

end;
end
else
begin
ch:=1;
For i:=1 to n-1 do
For k:=i+1 to n do
if a[k]<a[i] then
begin
m:=a[i];a[i]:=a[k];a[k]:=m;
end;
Writeln('Sap xep lai theo chieu tang dan:');
For i:=1 to n do
Write(a[i],' ');
Writeln;
end;
end;
Procedure chen;
begin
Repeat
Write('Ban hay nhap phan tu muon chen:');Readln(m);
n:=n+1;
k:=0;
For i:=1 to n do
if ((ch in [0,1]) and (a[i]>m)) or ((ch=2) and
(a[i]<m)) then
begin
k:=i;Break;
end;
if k=0 then a[n]:=m
else
begin
For j:=n downto k+1 do
a[j]:=a[j-1];
a[k]:=m;
end;
For i:=1 to n do
Write(a[i],' ');
Writeln;
Write('Ban co muon chen tiep khong?(c/k)');
Until Upcase(c)='K';
end;
Begin
clrscr;
nhap;
chen;
End.

3
4

Bi 4.
Program Bai4;
uses crt;
var
a,b:array[1..100] of string;
c:array[1..100,1..2] of word;
i,j,k,m,n:word;
s:string;
f:text;
Procedure chg(k:integer);
var
i,j:word;
c:char;
begin
b[k]:=a[k];
For i:=1 to length(b[k]) do
For j:=i+1 to length(b[k]) do
if b[k,j]<b[k,i] then
begin
c:=b[k,j];b[k,j]:=b[k,i];b[k,i]:=c;
end;
end;
Procedure xl;
var i:word;
begin
While Pos(' ',s)<>0 do
Delete(s,pos(' ',s),1);
If s[length(s)]=' ' then
Delete(s,length(s),1);
s:=s+' ';
if s[1]=' ' then
Delete(s,1,1);
While s<>'' do
begin
inc(n);
a[n]:=copy(s,1,pos(' ',s)-1);
Delete(s,1,pos(' ',s));
chg(n);
For i:=1 to n-1 do
if b[i]=b[n] then
Writeln(a[i],' ',a[n]);
end;
end;
Begin
clrscr;
Assign(f,'vanban.txt');
Reset(f);
i:=0;
n:=0;

3
5

## While not eof(f) do

begin
inc(i);
xl;
end;
Close(f);
End.

3
6

s 10
Bi 1.
{\$A+,B-,D+,E+,F-,G-,I+,L+,N-,O-,P-,Q-,R+,S+,T-,V+,X+}
{\$M 16384,0,655360}
Program bai1;
uses crt;
type matran=array[1..50,1..50] of integer;
var
A:matran;
n,i:integer;
procedure nhapmatran(var A:matran;n,m:integer);
var i,j:integer;
begin
for i:=1 to n do
for j:=1 to m do
begin
write('A[',i,',',j,']=');
end;
end;
procedure xuatmatran(var A:matran;n,m:integer);
var i,j:integer;
begin
for i:=1 to n do
begin
for j:=1 to m do write(A[i,j]:4);
writeln;
end;
end;
function timmax(i,j:integer):integer;
var max:integer;
begin
max:=A[i,j];
while i<=n do
begin
inc(i);inc(j);
if A[i,j]>max then max:=A[i,j];
end;
timmax:=max;
end;
begin
clrscr;
write('Nhap so hang hay cot cua ma tran: ');readln(n);
nhapmatran(A,n,n);clrscr;xuatmatran(A,n,n);
write('Cac max tren cac duong cheo can tim: ');
for i:=1 to n do write(' ',timmax(i,1));
end.

3
7

Bi 2.
{\$A+,B-,D+,E+,F-,G-,I+,L+,N-,O-,P-,Q-,R+,S+,T-,V+,X+}
{\$M 16384,0,655360}
Program bai3;
uses crt;
type matran=array[1..100,1..100] of integer;
var
A:matran;
n,m,x,y:integer;
t,k,i,j:integer;
procedure nhapmatran(var A:matran;n,m:integer);
var i,j:integer;
begin
for i:=1 to n do
for j:=1 to m do
begin
write('A[',i,',',j,']=');
end;
end;
procedure xuatmatran(var A:matran;n,m:integer);
var i,j:integer;
begin
for i:=1 to n do
begin
for j:=1 to m do write(A[i,j]:4);
writeln;
end;
end;
begin
clrscr;
nhapmatran(A,n,n);clrscr;xuatmatran(A,n,n);
write(' Nhap cot can xoa: ');readln(y);
t:=1; k:=1;
for i:=1 to n do
begin
for j:=1 to n do
if (i<>x) and (j<>y) then begin
A[t,k]:=A[i,j];inc(k); end;
if i<>x then inc(t);
if (k>n-1) and (i<n) then k:=1;
end;
xuatmatran(A,n-1,n-1);
end.

Bi 3.
Program bai4;

3
8

uses crt;
var
i,k,m,n:integer;
s,s2:string;
f:text;
Begin
Assign(f,'maso.inp');
Rewrite(f);
For i:=1 to 25 do
For k:=1 to 25 do
For m:=1 to 999 do
begin
Str(m,s);
While length(s)<3 do
s:='0'+s;
Write(f,chr(i+64)+chr(k+64)+s,' ');
n:=n+1;
if n mod 8=0 then
begin
Writeln(f);n:=0;
end;
end;
Close(f);
End.

3
9

s 11
Bi 1.
{\$A+,B-,D+,E+,F-,G-,I+,L+,N-,O-,P-,Q-,R+,S+,T-,V+,X+,Y+}
{\$M 16384,0,655360}
Program bai2;
uses crt;
type mang=array[1..100] of integer;
var
A:mang;
n,i,j,d,s:integer;
procedure nhapmang;
var i:integer;
begin
for i:=1 to n do
begin
end;
end;
procedure xuatmang;
var i:integer;
begin
for i:=1 to n do write(' ',A[i]);
writeln;
end;
begin
clrscr;
nhapmang;clrscr;xuatmang;
write('Do dai doan dai nhat cac phan tu lien tiep bang
nhau la: ');
s:=0;
for i:=n downto 2 do
begin
j:=1;
repeat
d:=1;
while (A[j]=A[j+1]) and (j<=i) do begin
inc(j);inc(d); end;
if d>s then s:=d;
inc(j);
until j>=i;
end;
writeln(s);
end.

4
0

## Bi 2. Nhn dng hng s thc (real con-stant)

Program sothuc;
uses crt;
var
i,k,m,n:integer;
s,s2:string;
f,f2:text;
Function kt(s:string):string;
var
b:string;
begin
b:='DUNG';
While s[1]=' ' do
Delete(s,1,1);
While s[length(s)]=' ' do
Delete(s,length(s),1);
if pos(' ',s)>0 then
b:='SAI'
else
begin
For i:=1 to length(s) do
begin
s[i]:=Upcase(s[i]);
if not (s[i] in ['0'..'9']) then
begin
if not (s[i] in ['+','-','E','.',',']) then b:='SAI';
if i=length(s) then b:='SAI';
if not ((s[i] in ['+','-']) and ((i=1) or (s[i1]='E'))) then b:='SAI';
if (s[i]='E') and (pos('.',s)>i) then b:='SAI';
if b='SAI' then Break;
end;
end;
if (pos('.',s)=0) and (pos('E',s)=0) then
b:='SAI'
else
begin
k:=0;
For i:=pos('.',s)+1 to length(s) do
if s[i] in ['0'..'9'] then
begin
k:=1;Break;
end;
if k=0 then b:='SAI';
if pos('E',s)=length(s) then b:='SAI';
if pos('.',s)=0 then Delete(s,pos('.',s),1);
if pos('E',s)=0 then Delete(s,pos('E',s),1);
if (pos('.',s)>0) or (pos('E',s)>0) then
b:='SAI'

4
1

end;
end;
kt:=b;
end;
Begin
clrscr;
Assign(f,'sothuc.inp');
Assign(f2,'sothuc.out');
Reset(f);
Rewrite(f2);
While not eof(f) do
begin
if pos('*',s)<>0 then break;
Writeln(f2,kt(s));
end;
Close(f);
Close(f2);
End.

Bi 3. Chui i xng.
program chuoi;
use crt;
var s:string;
f,f1:text;
a:array[1..200] of string;
Function dx(s:string):boolean;
var bl:boolean;
i:word;
Begin
bl:=true;
For i:=1 to length(s) do
If s[i] <> s[length(s)-i+1] then bl:=false;
dx:=bl;
End;
Function from(s:string;d:word):boolean;
var i:word;
bl:boolean;
Begin
bl:=false;
If d=0 then bl:=false
Else For i:=1 to d do
If s=a[i] then bl:=true;
from:=bl;

4
2

End;
Function xet(s:string):word;
var s1:string;
d,i,j:word;
Begin
fillchar(a,sizeof(a),0);
d:=0;
For i:=1 to length(s) do
For j:=1 to length(s)-i+1 do
Begin
s1:=copy(s,i,j);
If dx(s1) then
If not from(s1,d) then
Begin
inc(d);
a[d]:=s1;
End;
End;
xet:=d;
End;
Begin
assign(f,'chuoi.inp');
Reset(f);
assign(f1,'chuoi.out');
Rewrite(f1);
While not eof(f) do
Begin
Writeln(f1,xet(s));
End;
Close(f);
Close(f1);
End.

4
3

4
4