main.cpp

来自「汉诺塔的双递归调用」· C++ 代码 · 共 49 行

CPP
49
字号
#include<iostream.h>
void move2(int count, int start, int finish, int temp);
void move(int count, int start, int finish, int temp)
{
   if (count > 0) {
      move2(count - 1, start, temp, finish);
      cout << "Move disk " << count << " from " << start
           << " to " << finish << "." << endl;
      move2(count - 1, temp, finish, start);
   }
}

void move2(int count, int start, int finish, int temp)
{
	int swap; 
	while (count > 0) { 
		move(count - 1, start, temp, finish); 
		cout << "Move disk " << count << " from " << start
		<< " to " << finish << "." << endl;
		count--; 
		swap = start;
		start = temp;
		temp = swap;
	}
}

void main()
{ 
   int n;
   bool flag=true;
   char ch;
   cout<<"Please input number of disks ( > 0 ) to move:"<<endl;
   cin>>n;
   cin.get();
   while(flag && n>0)
   {
	   move2(n, 1, 3, 2);
	   cout<<"Continue(y/n)?"<<endl;
	   ch=cin.get();
	   if (ch=='y')
       {
		    cout<<"Please input number of disks ( > 0 ) to move:"<<endl;
            cin>>n;
            cin.get(); 
	   }
	   else
		   flag=false;
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?