2013年8月30日 星期五

[C/C++ 演算法]-河內塔<HanoiTower>

[C/C++ 演算法]-河內塔<HanoiTower>



剛才找資料時發現一個C/C++的教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:


http://openhome.cc/Gossip/AlgorithmGossip/


http://openhome.cc/Gossip/AlgorithmGossip/HanoiTower.htm





#include <iostream>
#include <cstdio>
/*
程式出處:http://openhome.cc/Gossip/AlgorithmGossip/HanoiTower.htm
*/
using namespace std;
void hanoi(int n, char A, char B, char C)
{
if(n == 1)
{
printf("Move sheet from %c to %c\n", A, C);
}
else
{
hanoi(n-1, A, C, B);
hanoi(1, A, B, C);
hanoi(n-1, B, A, C);
}
}
int main()
{
int n;
printf("請輸入盤數:");
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
return 0;
}





沒有留言:

張貼留言