汉诺塔

public class TowerOfHanoi 
{
	void hanoi(int n,char origin,char assist,char destination) 
	{
		if (n == 1)
		{
			move(n,origin,destination);
		} 
		else 
		{
			hanoi(n - 1,origin,destination,assist);
			move(n,origin,destination);
			hanoi(n - 1,assist,origin,destination);
		} 
	} 
	void move(int n, char a ,char b)
	{
		System.out.println(n+":"+a+"-->"+b);
	}	
	public static void main(String[] args) 
	{
		int n=4;
		char a='a',b='b',c='c';
		TowerOfHanoi tower = new TowerOfHanoi();
		tower.hanoi(n, a, b, c);		
	}
}

1:a-->b
2:a-->c
1:b-->c
3:a-->b
1:c-->a
2:c-->b
1:a-->b
4:a-->c
1:b-->c
2:b-->a
1:c-->a
3:b-->c
1:a-->b
2:a-->c
1:b-->c

你可能感兴趣的:(c,String,Class)