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, Towers);
return;
}
Hanoi(n - 1, source, 3 - source - target);
printf("Move ring %d from %c to %c\n", n, Towers, Towers);
Hanoi(n - 1, 3 - source - target, target);
}
int main(void)
{
int n;
while (scanf("%d", &n) != EOF)
{
Hanoi(n, 0, 2);
putchar('\n');
}
}
頁:
[1]