#include <iostream>
#include <stdlib.h>
using namespace std;
static int **pax, **pbx, **pcx;
static int cnt;//记录发生次数
void hannoi(int n, char a, char b, char c)
{
if (n < 1)
{
return;
}
else
{
hannoi(n - 1, a, c, b);
cout << a << "->" << c << endl;
hannoi(n - 1, b, a, c);
}
}
void showabc(int n)
{
//始终保持a b c数组输出其元素,方便查看
cout << "a " << "b " << "c " << endl;
for (int i = 0; i < n; i++)
{
cout << *(*pax + i) << " " << *(*pbx + i) << " " << *(*pcx + i) << " " << endl;
}
}
void movedata(int *x, int *y, int n)
{
//x -> y 数组x移动一个x顶部数据到y;始终是往y中顶部插入,x往顶部弹出一个
int tmp = 0;
tmp = x[0];
for (int i = 0; i < n - 1; i++)
{
x[i] = x[i + 1];
}
if (y[0] == 0)
y[0] = tmp;
else
{
int i = 0;
//确定y数组中为0的数组下标
while (y[i] != 0)
{
i++;
}
//开始移动y中所有数据整体往后移动一个
for (int j = i; j > 0; j--)
{
y[j] = y[j - 1];
}
//此时y中第一个空出来
i=0;
y[i]=tmp;
}
}
void hannoi(int n, int* a, int* b, int* c ,int num)
{
if (n < 1)
{
return;
}
else
{
hannoi(n - 1, a, c, b ,num);
{
cout << "***********" << cnt << "*****************" << endl;
if (a ==*pax)
cout << "a";
else if (a ==*pbx)
cout << "b";
else if (a == *pcx)
cout << "c";
cout <<"->";
if (c == *pax)
cout << "a";
else if (c == *pbx)
cout << "b";
else if (c == *pcx)
cout << "c";
cout<<endl;
movedata(a, c, num);
cnt++;
showabc(num);
}
hannoi(n - 1, b, a, c ,num);
}
}
int main(int argc, char *argv[])
{
int num;
cin >> num;
cout << "num=" << num << endl;
// hannoi(num, 'A', 'B', 'C');
//开辟比输入num大1的数组
int *pa = (int *)malloc(sizeof(int)*(num+1));
int *pb = (int *)malloc(sizeof(int)*(num + 1));
int *pc = (int *)malloc(sizeof(int)*(num + 1));
//数组清零操作
memset(pa, 0, sizeof(int)*(num + 1));
memset(pb, 0, sizeof(int)*(num + 1));
memset(pc, 0, sizeof(int)*(num + 1));
//输出各个数组首地址
cout << pa << " " << pb << " "<<pc<<endl;
pax = &pa;
pbx = &pb;
pcx = &pc;
cnt = 1;
//使用全局静态二级指针,为打印准备
cout << *pax << " " << *pbx << " " << *pcx << endl;
//初始化pa前num个数据
for (int i = 0; i<(num); i++)
{
pa[i] = i+1;
}
//默认pa num等于零预留空间
pa[num] = 0;
hannoi(num,pa,pb,pc,num+1);
delete pa,pb,pc;
system("pause");
return 0;
#include <stdlib.h>
using namespace std;
static int **pax, **pbx, **pcx;
static int cnt;//记录发生次数
void hannoi(int n, char a, char b, char c)
{
if (n < 1)
{
return;
}
else
{
hannoi(n - 1, a, c, b);
cout << a << "->" << c << endl;
hannoi(n - 1, b, a, c);
}
}
void showabc(int n)
{
//始终保持a b c数组输出其元素,方便查看
cout << "a " << "b " << "c " << endl;
for (int i = 0; i < n; i++)
{
cout << *(*pax + i) << " " << *(*pbx + i) << " " << *(*pcx + i) << " " << endl;
}
}
void movedata(int *x, int *y, int n)
{
//x -> y 数组x移动一个x顶部数据到y;始终是往y中顶部插入,x往顶部弹出一个
int tmp = 0;
tmp = x[0];
for (int i = 0; i < n - 1; i++)
{
x[i] = x[i + 1];
}
if (y[0] == 0)
y[0] = tmp;
else
{
int i = 0;
//确定y数组中为0的数组下标
while (y[i] != 0)
{
i++;
}
//开始移动y中所有数据整体往后移动一个
for (int j = i; j > 0; j--)
{
y[j] = y[j - 1];
}
//此时y中第一个空出来
i=0;
y[i]=tmp;
}
}
void hannoi(int n, int* a, int* b, int* c ,int num)
{
if (n < 1)
{
return;
}
else
{
hannoi(n - 1, a, c, b ,num);
{
cout << "***********" << cnt << "*****************" << endl;
if (a ==*pax)
cout << "a";
else if (a ==*pbx)
cout << "b";
else if (a == *pcx)
cout << "c";
cout <<"->";
if (c == *pax)
cout << "a";
else if (c == *pbx)
cout << "b";
else if (c == *pcx)
cout << "c";
cout<<endl;
movedata(a, c, num);
cnt++;
showabc(num);
}
hannoi(n - 1, b, a, c ,num);
}
}
int main(int argc, char *argv[])
{
int num;
cin >> num;
cout << "num=" << num << endl;
// hannoi(num, 'A', 'B', 'C');
//开辟比输入num大1的数组
int *pa = (int *)malloc(sizeof(int)*(num+1));
int *pb = (int *)malloc(sizeof(int)*(num + 1));
int *pc = (int *)malloc(sizeof(int)*(num + 1));
//数组清零操作
memset(pa, 0, sizeof(int)*(num + 1));
memset(pb, 0, sizeof(int)*(num + 1));
memset(pc, 0, sizeof(int)*(num + 1));
//输出各个数组首地址
cout << pa << " " << pb << " "<<pc<<endl;
pax = &pa;
pbx = &pb;
pcx = &pc;
cnt = 1;
//使用全局静态二级指针,为打印准备
cout << *pax << " " << *pbx << " " << *pcx << endl;
//初始化pa前num个数据
for (int i = 0; i<(num); i++)
{
pa[i] = i+1;
}
//默认pa num等于零预留空间
pa[num] = 0;
hannoi(num,pa,pb,pc,num+1);
delete pa,pb,pc;
system("pause");
return 0;
}
输入num=3
输入num =4
输入num=5