#include
#define discCount 4
long move(int, char, char,char);
int main()
{
long count;
count=move(discCount,'A','B','C');
printf("%d个盘子需要移动%ld次\n", discCount, count);
return 0;
}
long move(int n, char A, char B,char C)
{
long c1,c2;
if(n==1)
return 1;
else
{
c1=move(n-1,A,C,B);
c2=move(n-1,B,A,C);
return c1+c2+1;
}
}
#include
#define discCount 8
long move(int, char, char,char);
int main()
{
long count;
count=move(discCount,'A','B','C');
printf("%d个盘子需要移动%ld次\n", discCount, count);
return 0;
}
long move(int n, char A, char B,char C)
{
long c1,c2;
if(n==1)
return 1;
else
{
c1=move(n-1,A,C,B);
c2=move(n-1,B,A,C);
return c1+c2+1;
}
}
#include
#define discCount 16
long move(int, char, char,char);
int main()
{
long count;
count=move(discCount,'A','B','C');
printf("%d个盘子需要移动%ld次\n", discCount, count);
return 0;
}
long move(int n, char A, char B,char C)
{
long c1,c2;
if(n==1)
return 1;
else
{
c1=move(n-1,A,C,B);
c2=move(n-1,B,A,C);
return c1+c2+1;
}
}
#include
#define discCount 20
long move(int, char, char,char);
int main()
{
long count;
count=move(discCount,'A','B','C');
printf("%d个盘子需要移动%ld次\n", discCount, count);
return 0;
}
long move(int n, char A, char B,char C)
{
long c1,c2;
if(n==1)
return 1;
else
{
c1=move(n-1,A,C,B);
c2=move(n-1,B,A,C);
return c1+c2+1;
}
}
#include
#define discCount 24
long move(int, char, char,char);
int main()
{
long count;
count=move(discCount,'A','B','C');
printf("%d个盘子需要移动%ld次\n", discCount, count);
return 0;
}
long move(int n, char A, char B,char C)
{
long c1,c2;
if(n==1)
return 1;
else
{
c1=move(n-1,A,C,B);
c2=move(n-1,B,A,C);
return c1+c2+1;
}
}
调试结果: