汉诺塔问题

如下面代码:

public class HanoiTest 
{
	private static int size = 3;
	
	public static void main(String[] args)
	{
		Hanoi(size, 'A', 'B', 'C');
	}
	
	public static void Hanoi(int num, char from, char ch, char to)
	{
		if(num == 1)
			System.out.println("Moving disk 1 from " + from + " to " + to);
		else
		{
			Hanoi(num-1, from, to, ch);
			
			System.out.println("Moving disk " + num + " from " + from + " to " + to);
			
			Hanoi(num-1, ch, from, to);
		}
	}
}

 

 

输出如下:

 

Moving disk 1 from A to C
Moving disk 2 from A to B
Moving disk 1 from C to B
Moving disk 3 from A to C
Moving disk 1 from B to A
Moving disk 2 from B to C
Moving disk 1 from A to C

 

你可能感兴趣的:(C++,c,C#)