Skip to content

Instantly share code, notes, and snippets.

@wfwei
Last active December 14, 2015 02:09
Show Gist options
  • Save wfwei/5011558 to your computer and use it in GitHub Desktop.
Save wfwei/5011558 to your computer and use it in GitHub Desktop.
汉诺塔问题递归算法----一个庙里有三个柱子,第一个有64个盘子,从上往下盘子越来越大。要求庙里的老和尚把这64个盘子全部移动到第三个柱子上。移动的时候始终只能小盘子压着大盘子。而且每次只能移动一个。
#include "stdio.h"
void move(int id, char f, char t){
printf("move %d from %c to %c\n", id, f, t);
}
void hanoi(int n, char a, char b, char c){
if(n==1){
move(n, a, c);
}else{
hanoi(n-1, a, c, b);
move(n, a, c);
hanoi(n-1, b, a, c);
}
}
int main(){
int n;
while(printf("input this number of pillars:")&&scanf("%d", &n)!=EOF && n>0){
hanoi(n, 'a', 'b', 'c');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment