汉诺塔(hanoi tower)游戏

问题

递归实现汉诺塔

算法实现

#include 
void hanoi( int n, char a, char b, char c)
{
  if( n > 0 ) {
    hanoi(n-1, a, c, b);
    printf("MOVE %d: %c -> %c\n", n, a, c);
    hanoi(n-1, b, a, c);
  }
}
main( )
{
  int n;
  printf("Please input the number of hanoi tower:");
  scanf("%d", &n);
  hanoi(n, 'A', 'B', 'C');
}

你可能感兴趣的:(#,算法基础实例)