thaphnko

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

//hn_unit1 {Thap Ha Noi khu de quy}

unit hn_unit;

interface

const max=20;

type elementtype = record

n,a,b,c:integer

end;

var dem,n,a,b,c:integer;

s:array[1..max] of elementtype;

last:integer;

procedure push;

function empty:boolean;

procedure pop;

procedure top;

procedure print(a,b:integer);

procedure hanoi(n,a,b,c:integer);

implementation

procedure push;

begin

inc(last);s[last].n:=n;s[last].a:=a;

s[last].b:=b;s[last].c:=c

end;

function empty:boolean;

begin

empty:=(last<=0)

end;

procedure pop;

begin

s[last].n:=0;s[last].a:=0;s[last].b:=0;

s[last].c:=0;last:=last-1

end;

procedure top;

begin

n:=s[last].n;a:=s[last].a;b:=s[last].b;

c:=s[last].c

end;

procedure print(a,b:integer);

begin

inc(dem);writeln(dem:4,a:5,'->',b:1)

end;

procedure hanoi(n,a,b,c:integer);

begin

if n=1

then print(a,b)

else begin

hanoi(n-1,a,c,b);

print(a,b);

hanoi(n-1,c,b,a)

end

end;

begin

end.

-----------------------------------------------------

//hn_unit2

USES HN_UNIT;

BEGIN

WRITE('N=');

READLN(N);

A:=1; B:=2; C:=3; DEM:=0;

LAST:=0; (*MAKE NULL *)

PUSH;

REPEAT

WHILE N>1 DO

BEGIN

PUSH;

N:=N-1; B:=C; C:=6-A-B;

END;

PRINT(A,B);

TOP;POP;

IF NOT EMPTY THEN PRINT(A,B);

N:=N-1; A:=C; C:=6-A-B;

UNTIL EMPTY;

READLN; END.

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