Visual Basic

Module Module1
    Function ha(ByVal n As Integer, ByVal from As Integer, ByVal too As Integer, ByVal temp As Integer) As Integer
        If n = 1 Then
            Console.WriteLine(from & "->" & too)
        Else
            ha(n - 1, from, temp, too)
            ha(1, from, too, temp)
            ha(n - 1, temp, too, from)
        End If
    End Function

    Sub Main()
        Dim n As Integer
        n = Console.ReadLine()
        ha(n, 1, 3, 2)
        Console.ReadKey()
    End Sub

End Module

 

C language

#include <stdio.h>
void towers(int,char,char,char); 
void main(void) 
{
 int n; 
 printf("Enter the number of the disk:");
 scanf("%d",&n); 
 towers(n,'A','C','B');
 getch();
 return 0;
}

void towers(int n,char from,char to,char temp)
{
 if(n==1)
 { 
  printf("move disk 1 from %c to %c\n",from,to);
  return;
  }
  towers(n-1,from,temp,to);
  printf("move disk %d from %c to %c\n",n,from,to);
  towers(n-1,temp,to,from);
}
arrow
arrow
    全站熱搜

    okplaymayday 發表在 痞客邦 留言(0) 人氣()