horsemanship世界

星期一, 6月 12, 2006

Lab16

public class recursive {
public static void main(String[] args)
{
recursive(3,1,2,3);
}
public static void recursive (int i,int one,int two,int three ){
if(i==1) {
System.out.println("Move dis"+1+" From "+one+" to "+three);
}
else{
recursive ( i-1, one, three, two );
System.out.println("Move dis"+i+" From "+one+" to "+three);
recursive ( i-1, two, one, three );
}}}
結果:

Lab15

import java.io.*;
public class homework {
public static void main(String args[]) throws IOException {
int[] num = new int[5];
int i, j,temp;
System.out.println("請輸入五個數字:");
for (i=0;i<5;i++)
{
BufferedReader input = new BufferedReader(new InputStreamReader(
System.in));
num[i] = Integer.parseInt(input.readLine());
}
for (i=0;i<5;i++)
{
for (j=0;j<5;j++)
{
if (num[i] > num[j]) {
temp = num[i];
num[i] = num[j];
num[j] = temp;
}}}
for (i=0; i<5;i++)
{
System.out.print(" " + num[i] + " ");
}}}

結果:
請輸入五個數字:
12
46
98
82
53
98 82 53 46 12

Lab14

public class recursive {
public static void main(String[] args) {
System.out.println(recursive(5));
}
public static int recursive (int i){
if(i==1) {
return (1);
}
else{
return (recursive(i-1)+i*i);
}}}

結果:

星期一, 6月 05, 2006

Homework8

package ten;
public class matrix {
public static void main(String[] args)
{
int x , y ;
int box;
int[]age={7,6,9,2,5};
for(x=0;x<5;x++) y="x+1;y<5;y++)">age[x])
{
box=age[x];
age[x]=age[y];
age[y]= box ;}}
System.out.println(age[x]*age[x]);}}}


結果:

Lab13

public class ArrayOfScores
{
public static void main(String[] args)
{
int i,j,temp;
int[] score={1,4,8,5,6};

for (i=0;i<5;i++)
{
for (j=i+1;j<5;j++)
{
if (score[j]>score[i])
{
temp=score[i];
score[i]=score[j];
score[j]=temp;
}
}
System.out.println(score[i]);
}


}
}