//---------------------------------------------------------------------------
#include <stdio.h>
#pragma hdrstop
#include <tchar.h>
//---------------------------------------------------------------------------
#pragma argsused
#define N 5
void move(int n, int *a, int *b, int *c);
int gettop(int *arr, int n);
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
int a[N] = {5, 4, 3, 2, 1};
int b[N] = {0};
int c[N] = {0};
for (i=N-1; i>=0; i--)
{
printf("%d\t%d\t%d\n", a[i], b[i], c[i]);
}
printf("a\tb\tc\n");
printf("转移后:\n");
move(N, a, b, c);
for (i=N-1; i>=0; i--)
{
printf("%d\t%d\t%d\n", a[i], b[i], c[i]);
}
printf("a\tb\tc\n");
getchar();
return 0;
}
int gettop(int *arr, int n)
{
int i = 0;
for (i=0; i<n; i++)
{
if (arr[i] == 0)
{
return i;
}
}
return n;
}
void move(int n, int *a, int *b, int *c)
{
int temp1, temp2;
if (n >= 1)
{
move(n-1, a, c, b); //先将a上的n-1个通过c移动到b
temp1 = gettop(a, N); //将最后一个移动到c
temp2 = gettop(c, N);
c[temp2] = a[temp1-1];
a[temp1-1] = 0;
move(n-1, b, a, c); //最后将b上的n-1个通过a移动到c
}
}
//---------------------------------------------------------------------------