星期日, 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);
}
}

星期六, 1月 07, 2006

Recursion

星期三, 1月 04, 2006

lab-0102

import java.io.*;
public class lab_0102 {
public static void main(String args[]) throws IOException {
double[] num = new double[5];
int a, b;
double temp;
System.out.println("Enter some number:");
for (a = 0; a <>
BufferedReader input = new BufferedReader(new InputStreamReader(
System.in));
num[a] = Integer.parseInt(input.readLine());
}
for (a = 0; a <>
for (b = 0; b <>
if (num[a] > num[b]) {
temp = num[a];
num[a] = num[b];
num[b] = temp;
}
}
}
for (a = 0; a <>
System.out.print("" + num[a] + " ");
}
System.out.println("");
System.out.println("Largest to Smallest.");
System.exit(0);
}
}

星期日, 1月 01, 2006

lab_2













測試結果