[ZJ]a227: 三龍杯 -> 河內之塔
題目:
http://zerojudge.tw/ShowProblem?problemid=a227
==================================================================
#include<stdio.h>
int tower(int n,char a,char b,char c)
{
if(n>0)
{
tower(n-1,a,c,b);
printf("Move ring %d from %c to %c\n",n,a,c);
tower(n-1,b,a,c);
}
}
int main(void)
{
int n;
while(scanf("%d",&n)!=EOF)
{
tower(n,'A','B','C');
}
return 0;
}
http://zerojudge.tw/ShowProblem?problemid=a227
==================================================================
#include<stdio.h>
int tower(int n,char a,char b,char c)
{
if(n>0)
{
tower(n-1,a,c,b);
printf("Move ring %d from %c to %c\n",n,a,c);
tower(n-1,b,a,c);
}
}
int main(void)
{
int n;
while(scanf("%d",&n)!=EOF)
{
tower(n,'A','B','C');
}
return 0;
}
留言
張貼留言