冰楓論壇
標題:
Online Judge a227. 三龍杯 -> 河內之塔
[打印本頁]
作者:
kkmomo
時間:
2015-7-4 23:54
標題:
Online Judge a227. 三龍杯 -> 河內之塔
題目
http://zerojudge.tw/ShowProblem?problemid=a227
解一下題目找回信心T_T
#include <stdio.h>
#define Towers "ABC"
void Hanoi(int n, int source, int target)
{
if (n == 1)
{
printf("Move ring %d from %c to %c\n", n, Towers[source], Towers[target]);
return;
}
Hanoi(n - 1, source, 3 - source - target);
printf("Move ring %d from %c to %c\n", n, Towers[source], Towers[target]);
Hanoi(n - 1, 3 - source - target, target);
}
int main(void)
{
int n;
while (scanf("%d", &n) != EOF)
{
Hanoi(n, 0, 2);
putchar('\n');
}
}
複製代碼
歡迎光臨 冰楓論壇 (https://bingfong.com/)
Powered by 冰楓