星期日, 1月 15, 2006

tower

import java.io.*;
public class Tower
{
public static void Tower(int n,char frompg,char topg,char auxpg)
{
if(n==1)
System.out.println("Move disc 1 "+frompg+" --> "+topg);
else
{
Tower(n-1,frompg,auxpg,topg);
System.out.println("Move disc "+n+" "+frompg+" --> "+topg);
Tower(n-1,auxpg,topg,frompg);
}
}
public static void main(String[] args)throws IOException
{
int n;
BufferedReader keyboard =new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入n個tower:");
n=Integer.parseInt(keyboard.readLine());
char X='X',Y='Y',Z='Z';
if(n==0)
System.out.println("請重新輸入");
else
Tower(n,X,Y,Z);
}
}

0 Comments:

張貼留言

<< Home