void move(char pos1, char pos3)
{
printf("%c->%c ", pos1, pos3);
}
void hanoi(int n,char pos1,char pos2,char pos3)
{
if (n <= 2)
{
move(pos1, pos3);
}
else
{
hanoi(n - 1, pos1, pos3, pos2);
move(pos1, pos3);
hanoi(n - 1, pos2, pos1, pos3);
}
}
int main()
{
hanoi(1, 'A', 'B', 'C');
printf("\n");
hanoi(2, 'A', 'B', 'C');
printf("\n");
hanoi(3, 'A', 'B', 'C');
return 0;
}
问题梳理:
1.主函数中多此一举地定义了n=0,printf(n),导致输出有误
2.move函数中%c写成%d,而且还加了换行