題目:魔術方塊                                                                              程式檔名:test15.java

說明:輸入階數n(n<=15,且為奇數),印出之魔術方塊,該方塊之各列和各行和與對角線之和均相等。程式中必須有須有判斷範圍的程式,若是超出題目所訂定的數值範圍則要求重新輸入。輸入“0”時即結束此程式。

畫面輸出結果:

請輸入階數:17

超出範圍,重新輸入

 

請輸入階數:4

超出範圍,重新輸入

 

請輸入階數:5

輸出結果

17        24    1      8      15   

23        5      7      14    16   

4      6     13    20    22   

10        12    19    21    3     

11       18    25    2      9

 

請輸入階數:7

輸出結果

30        39    48    1      10    19    28   

38        47    7      9      18    27    29   

46        6      8      17    26    35    37   

5          14    16    25    34    36    45   

13        15    24    33    42    44    4     

21        23    32    41    43    3      12   

22        31    40    49    2      11    20   

請輸入階數:0     ß 程式結束

 

--------------------------------------

 

 

public class t0618 {
static int n = 5;
static int i = 0;
static int x = n / 2;
static int y = n - 1;
static int tempy, tempx;

 

public static void main(String[] args) {
int c[][] = new int[17][17];
c[y][x] = 1;
for (i = 2; i <= n * n; i++) {
tempy = (y + 1) % n; // 超過格子時,Xn取餘數歸零,例如y+1=5%5=0
tempx = (x + 1) % n;
if (c[tempy][tempx] == 0) {
y = tempy; // 右上無值,y往右上移動
x = tempx;
c[y][x] = i;
} else { //代表右上有值,y往下移動
y=y-1; //=y--
c[y][x]=i;
}
}
for(y=n-1 ; y>=0 ; y--){
for(x=0; x<=4; x++){
System.out.print(c[y][x]+"\t");
}
System.out.println();
}
}

 

}

---Console

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

 

 

 -----------------------------------不考

 

--------------------------------------------------------------------

 ---第三題

import java.io.*;
public class test3 {
public static void main(String[] args) throws IOException {
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
while (true) { // 無窮迴圈
String str = buf.readLine(); // 鍵盤輸入一行

if (str.length() > 15) { // str.length() = 字串長度
System.out.println("範圍錯誤");
continue; // 跳回迴圈開頭
}

if (str.equals("0")) { // equals = 比對,也有可能井字號時結束
System.out.println("結束程式");
return; //結束副程式
}

int a[]=new int[str.length()],i,sum=0;
for(i=0;i<str.length();i=i+2)
{ a[i]=str.charAt(i)-48; }
for(i=0;i<str.length();i++)
{ if(str.charAt(i)=='*')
{ //乘的前後字元相乘,乘積放在後項
a[i+1]=a[i-1]*a[i+1];
a[i-1]=0;//前項歸零
}
}
for(i=0;i<str.length();i=i+2)//索引編號是偶數的元素做加總
{ sum+=a[i]; }
System.out.println("運算式結果為" + sum);
}
}
}
/* 編號i= 01234567890
例如 str=" 2+6*8*2+5*9"
a[i]= 2 6 8 2 5 9//a[i]取編號為02468
//i=3時,a[4]=48,a[2]=0
//i=5時,a[6]=96,a[4]=0
//i=9時,a[10]=45,a[8]=0
a[0]=2,a[2]=0,a[4]=0,a[6]=96,a[8]=0,a[10]=45

 

---Console

 

1+2*5
運算式結果為11
1+2*5*4+5*3
運算式結果為56

 

 ---第四題

import java.io.*;
import java.util.*;
public class test4 {

public static void main(String args[])throws IOException
{
Random rd=new Random();//建立亂數物件 Random為類別,rd為Random的物件
int a[]=new int[51];//編號1到編號50
int i=0,count=0,n=0;
System.out.println("排序前");
while(true)
{
n=rd.nextInt(50);//抽出0到49的數字
n=n+1;//+1後,n的值為 1到50
if(a[n]==0)//a[n]==0表示,n這個數字尚未被選取
{
System.out.print(n +"\t");
count=count+1;//抽出的數目+1
a[n]=1;//a[n]=1表示這個數字已被選取,不能再被選
if(count%10==0){System.out.println();}//抽出10的倍數要換行
if(count==40 ){break;}//抽出40個跳出迴圈
}

}
System.out.println("排序後"+"\n");

for(n=1;n<=50;n++)
{
if(a[n]==1)
{
System.out.print(n +"\t");
count=count+1;

if(count%10==0){System.out.println();}
if(count==40 ){break;}
}
}
}
}

---Console

 

排序前
29 40 41 20 14 24 17 25 12 6
18 5 39 50 16 36 10 46 4 32
48 2 38 30 34 42 26 35 33 45
1 47 19 37 44 22 49 11 21 13
排序後

1 2 4 5 6 10 11 12 13 14
16 17 18 19 20 21 22 24 25 26
29 30 32 33 34 35 36 37 38 39
40 41 42 44 45 46 47 48 49 50

 

 

 

arrow
arrow
    全站熱搜

    玥君 發表在 痞客邦 留言(0) 人氣()