[C++] - (递归)汉诺塔
注:来源于教科书
#include "iostream.h"
void main(){
int n;
void hanoi(int n,char a,char b,char c);
cout << "Please input the number of disks to be moved:" << endl;
cin >> n;
hanoi(n,'a','b','c');
}
void hanoi(int n,char a,char b,char c){
if(n>0){
hanoi(n-1,a,c,b);
cout << "\nMove disc " << n << " from pile " << a << " to " << b;
hanoi(n-1,c,b,a);
}
}
void main(){
int n;
void hanoi(int n,char a,char b,char c);
cout << "Please input the number of disks to be moved:" << endl;
cin >> n;
hanoi(n,'a','b','c');
}
void hanoi(int n,char a,char b,char c){
if(n>0){
hanoi(n-1,a,c,b);
cout << "\nMove disc " << n << " from pile " << a << " to " << b;
hanoi(n-1,c,b,a);
}
}
个性签名:做要做好,做到不三不四不如不做。