Исходник на Си++
#include #include void print_move( int from,int to ) { cout<<"Move Disk From "<}
void transfer ( int from,int to,int via,int n ) { if(n==1) { print_move(from,to); } else { transfer(from,via,to,n-1); print_move(from,to); transfer(via,to,from,n-1); } }
void main(int k,char**s) { if(k>1&&atoi(s[1])) { transfer( 1,3,2,atoi(s[1])); } }
Исходник на Си
#include #include char a,b,c; int num; void hanoy(int num,char a,char b,char c){ if(num>0){ hanoy(num-1,a,c,b); printf("%c--->%c\n",a,c); hanoy(num-1,b,a,c); } } void main(){ clrscr(); printf("number of rings="); scanf("%d",&num); a='A';b='B';c='C'; hanoy(num,a,b,c); getch(); }
Исходник на Паскале
procedure Solve(n: integer; a,b,c: Char); begin if n > 0 then begin Solve(n-1, a, c, b); Writeln('Переместить диск со стержня ', a, ' на стержень ',b); Solve(n-1, c, b, a); end; end;
begin Solve(4, '1','2','3'); end.
Исходник на Бэйсике
10 dim A#1, B#1, C#1 20 input "Сколько колец?", N 30 gosub 1(N, "A", "B", "C"): print: end 40 def fn 1(N, X#, Y#, Z#) 50 A# = X#: B# = Y#: C# = Z# 60 if N>1 then 80 70 print A#; "->"; c#; " "; :goto 120 80 N1 = N: gosub 1(N1 - 1, A#, C#, B#); 90 print A#; "->"; b#; " "; 100 gosub 1(n1 - 1, C#, A#, B#) 110 N1 = N1 + 1: A1# = B#: B# = A1# 120 return
1 2
8 8 8
| |