[C How To Program] 习题5.39 汉诺塔

来源:互联网 发布:nba2k周琦数据 编辑:程序博客网 时间:2024/06/08 03:20
#include <stdio.h>int count = 0;void hanio( int );void move( int, int );void doh( int, int, int, int );int main(){int i;for(i = 1;i <= 10; i++){count = 0;hanio(i);printf("%d of hanio :%d times of move\n", i, count);}}/*  |     |     | *  1  2     3 * _|_   _|_   _|_ */void hanio( int n ){doh(n , 1, 2, 3);}void doh(int n, int a, int b, int c){if(n == 1){move(a, c);return;}doh(n - 1, a, c, b);move(a, c);doh(n - 1, b, a, c);}void move( int a, int b){count ++;printf("%d -> %d\n",a, b);}

0 0