網頁

2012年7月20日 星期五

a227. 三龍杯 -> 河內之塔

/**********************************************************************************/
/*  Problem: a227 "三龍杯 -> 河內之塔" from 2011三龍杯 (成附建杯)   */
/*  Language: C (429 Bytes)                                                       */
/*  Result: AC(9ms, 298KB) judge by this@ZeroJudge                                */
/*  Author: monkey413 at 2012-07-20 21:16:26                                      */
/**********************************************************************************/
#include<iostream>
void hanoi(int n, char A, char B, char C)
{
    if(n==1) printf("Move ring %d from %c to %c\n",n,A,C) ;
    else
    {
        hanoi(n-1,A,C,B) ;
        printf("Move ring %d from %c to %c\n",n,A,C) ;
        hanoi(n-1,B,A,C) ;
    }
}
int n;
int main()
{
    while(scanf("%d",&n)==1)
    {
        hanoi(n,'A','B','C');
        printf("\n") ;
    }
    return 0;
}
這題讓我瞭解到了遞迴的強大了!!

沒有留言:

張貼留言