2000 - CÁC VÒNG TRÒN OLYMPIC(DÀNH CHO HỌC SINH THCS VÀ PTTH){$Q...

Bài 45/2000 - Các vòng tròn Olympic

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

{$Q-}

{$M 65000 0 655360}

Program Vong_Tron;

Uses Crt,Dos;

Const Max = 39;

Fileout = 'VTron.out';

Dvt : array [1 .. 5,0 .. 8] of byte = ((8,1,2,3 ,4 ,5 ,6 ,7,8),

(6,2,3,4 ,9 ,10,11,0,0),

(6,4,5,6 ,11,12,13,0,0),

(4,6,7,13,14,0 ,0 ,0,0),

(4,1,2,9 ,15,0 ,0 ,0,0));

D0 : array [1 .. 5] of byte = (8,11,13,14,15);

Type Limt = 0 .. Max;

Mang = array [Limt] of byte;

Var A,B : Mang;

dm : longint;

fout : text;

{---}

Procedure Time;

Var h,k,i,j : word;

Begin

Gettime(h,k,i,j);

writeln(h,' : ',k,' : ',i,'.',j);

End;

Procedure Output;

Var i,j : byte;

Inc(dm);

For i := 1 to 15 do write(fout,A[i],' ');

writeln(fout);

Function GT(j0,count : shortint) : byte;

Var s,i0 : shortint;

s := 0;

For i0 := 1 to Dvt[j0,0] do

if Dvt[j0,i0] <= count then Inc(s,A[Dvt[j0,i0]]);

GT := s;

Procedure Try(s0,count,k0 : shortint);

Var i0 : shortint;

Begin

if (count <= D0[k0]) and (s0 <= Max) then

For i0 := 1 to Max-s0 do if B[i0] = 0 then

Begin

B[i0] := 1;

A[count] := i0;

if (count = D0[k0]) and (s0 + i0 = Max) then

Begin

if k0 = 5 then Output else Try(gt(k0 + 1,count),count + 1,k0 + 1);

End else Try(s0 + i0,count + 1,k0);

B[i0] := 0;

End;

End;

{---}

Procedure Process;

clrscr;

Time;

Assign(fout,fileout);rewrite(fout);

Fillchar(A,sizeof(A),0);

B:= A; dm := 0;

Try(0,1,1);

writeln(fout,'So cach : ',dm);

close(fout); Time;

End;

BEGIN

Process;

END.

Cách ghi kết quả trong file Vtron.out như sau: trong mỗi dòng ghi một cách đặt các số theo thứ tự từ 1 đến

15 theo cách đánh số như trên hình vẽ. Số cách xếp được ghi ở cuối tệp.

(Lời giải của bạn Đỗ Thanh Tùng - Lớp 12 Tin - PTTH chuyên Thái Bình)