2001 - XÁC ĐỊNH CÁC TỨ GIÁC ĐỒNG HỒ TRONG MA TRẬN (DÀNH CHO HỌC...

Bài 52/2001 - Xác định các tứ giác đồng hồ trong ma trận

(Dành cho học sinh THCS và PTTH)

uses crt;

var s,n,i,k,j,a1,a2,b1,b2:integer;

chon,mau:byte;

a:array[1..100,1..100]of integer;

{---}

procedure nhap;

begin

write('nhap n>=2:');readln(n);

for i:=1 to n do

for j:=1 to n do

begin

write('nhap a[',i,'j]:');

readln(a[i,j]);

end;

end;

procedure tinh;

clrscr;

nhap;

s:=0;

for i:=1 to n-1 do

for j:=1 to n-1 do

if ((a[i,j]<a[i,j+1])and(a[i,j+1]<a[i+1,j+1])and(a[i+1,j+1]<a[i+1,j]))

or((a[i,j+1]<a[i+1,j+1])and(a[i+1,j+1]<a[i+1,j])and(a[i+1,j]<a[i,j]))

or((a[i+1,j+1]<a[i+1,j])and(a[i+1,j]<a[i,j])and(a[i,j]<a[i,j+1]))

or((a[i+1,j]<a[i,j])and(a[i,j]<a[i,j+1])and(a[i,j+1]<a[i+1,j+1]))

then inc(s);

writeln;

writeln('So luong tu giac dong ho la:',s);

readln;

procedure max;

var t:integer;

writeln('Nhap n>=2:');readln(n);

i:=1;

a1:=1;a2:=n;

b1:=1;b2:=n;

mau:=0;

t:=0;

while i<=n*n do

begin

for k:=a1 to a2 do

begin

a[b1,k]:=i;

gotoxy(5*k,b1);

inc(mau);

if mau>15 then mau:=1;

textcolor(mau);

write(i);

delay(70);inc(i);

end;

for k:=b1+1 to b2+t do

a[k,a2]:=i;

gotoxy(5*(a2),k);

inc(mau);

if mau>15 then

mau:=1;

textcolor(mau);

write(i);

delay(70);

inc(i);

end;

for k:=b2+t downto b1+1 do

a[k,b2]:=i;

gotoxy(5*(b2-1),k);

if mau>15 then mau:=1;

for k:=a2-2 downto a1 do

a[b1+1,k]:=i;

gotoxy(5*k,b1+1);

dec(a2,2);

dec(b2,2);

inc(t,2);

inc(b1,2);

end;

if n>2 then s:=3*(n-2) else s:=1;

writeln;writeln;

writeln('Bang dong ho max');writeln;

writeln('Voi ma tran vuong cap ',n,'thi so luong tu giac dong ho lon nhat la:',s);

End;

procedure min;

writeln('n>=2:');readln(n);

b1:=1;

for k:=1 to n do

a[b1,k]:=i;

gotoxy(5*k,b1);

inc(b1);

writeln;writeln;writeln('Bang tren s co gia tri=0');

BEGIN

Clrscr;

repeat

textcolor(white);

writeln('1:cau a (Tinh so luong S)');

writeln('2:cau b (Lap bang co S lon nhat)');

writeln('3:cau c (Lap bang co S nho nhat)');

writeln('4:thoat');

writeln('Chon chuc nang:');readln(chon);

case chon of

1: begin

clrscr;

tinh;

end;

2: begin

max;

3: begin

clrscr;

min;

end;{of Case}

clrscr;

until chon=4;

END.

(Lời giải của bạn:Nguyễn Việt Hoà)