标签:
经典递归问题----汉诺塔问题
#include <stdio.h>
#include <stdlib.h>
void move(int i, int from, int to){
printf("move %d from %d to %d\n", i, from, to);
}
void hanoi(int n, int from, int help, int to){ //use ‘help‘ to move ‘from‘ to ‘to‘.
if(n == 1){
move(n, from, to);
}else{
hanoi(n-1, from, to, help);
move(n, from, to);
hanoi(n-1, help, from, to);
}
}
复杂度分析:T(n) = 2T(n-1)+1 ==> T(n)=2^n-1;
标签:
原文地址:http://www.cnblogs.com/informatics/p/5093425.html