2012年10月19日 星期五

巧合數計算


3025這個數字有個巧合,如果將這數字看成字串,將其分為兩半,每一半也都是一
個十位數,亦即30和25,則其和平方
(30+25)^2 = 3025
又變成原來的數字了。本題是找出這種類似的巧合數字出來,程式要輸入一個
位數(2、4、6、或8),然後找出該位數的所有類似巧合的數字。例如4位數,從
0000到9999。請注意,0也要算在內,也就是說0001也等於(00+01)^2,也是4位數
中的巧合數。
例如:
輸入位數 : 2
輸出 : 00
01
81

Java解法


import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Midterm_3 {

public static void main(String[] args) {

String m;
int n = 0;
int sum = 0;
int chksum = 0;
String msg = "";

do {
m = JOptionPane.showInputDialog("請輸入測試位數(偶數且為個位數)");
n = Integer.valueOf(m);
} while (n <= 0 || n > 9 || n % 2 == 1);

switch (n) {
case 2:
DecimalFormat f2 = new DecimalFormat("00");
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
sum = (int) Math.pow(i + j, 2);

if (sum == chksum)
msg = msg + f2.format(sum) + "\n";
chksum++;
}

}
JOptionPane.showMessageDialog(null, "巧合數為 :" + "\n" + msg, "查詢結果",
1);
break;
case 4:
DecimalFormat f4 = new DecimalFormat("0000");
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
sum = (int) Math.pow(i + j, 2);

if (sum == chksum)
msg = msg + f4.format(sum) + "\n";
chksum++;
}

}
JOptionPane.showMessageDialog(null, "巧合數為 :" + "\n" + msg, "查詢結果",
1);
break;
case 6:
DecimalFormat f6 = new DecimalFormat("000000");
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
sum = (int) Math.pow(i + j, 2);

if (sum == chksum)
msg = msg + f6.format(sum) + "\n";
chksum++;
}

}
JOptionPane.showMessageDialog(null, "巧合數為 :" + "\n" + msg, "查詢結果",
1);
break;
case 8:
DecimalFormat f8 = new DecimalFormat("00000000");
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
sum = (int) Math.pow(i + j, 2);

if (sum == chksum)
msg = msg + f8.format(sum) + "\n";
chksum++;
}

}
JOptionPane.showMessageDialog(null, "巧合數為 :" + "\n" + msg, "查詢結果",
1);
break;
}
}

}


2012年10月2日 星期二

河洛之數 -JAVA入門練習

如果矩陣再大可能就要用到陣列,但考試的話,這樣解題簡單多了
設計3*3矩陣 數字由1-9不能重複,每次產生時矩陣排列都會有變化,但是縱橫交叉和都要為15

 public class JVD104 {

    public static void main(String[] args) {
        int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        boolean redo = true;
               do {
            // 1.交換陣列數字內容
            for (int i = 1; i <= 9; i++) {
                int j = (int) (Math.random() * 9) + 1;
                int temp;
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
            // 2.加總8種相加的可能(橫3縱3斜2)
            int v1 = a[1] + a[2] + a[3];
            int v2 = a[4] + a[5] + a[6];
            int v3 = a[7] + a[8] + a[9];
            int h1 = a[1] + a[4] + a[7];
            int h2 = a[2] + a[5] + a[8];
            int h3 = a[3] + a[6] + a[9];
            int y1 = a[1] + a[5] + a[9];
            int y2 = a[3] + a[5] + a[7];
            if (v1 == 15 && v2 == 15 && v3 == 15 && h1 == 15 && h2 == 15
                    && h3 == 15 && y1 == 15 && y2 == 15)
                redo = false;
        } while (redo); // false就跳開
               // 3.輸出結果
        System.out.println("答案為: ");
        System.out.println(a[1] + " " + a[2] + " " + a[3]);
        System.out.println(a[4] + " " + a[5] + " " + a[6]);
        System.out.println(a[7] + " " + a[8] + " " + a[9]);
        System.out.println("不論橫向縱向斜對角數字和均為15");
            }

}

2012年10月1日 星期一

亂數排序器 JAVA入門練習

1.自行輸入要產生的亂數個數
2.輸入任何一個數值N後要顯示出來
3.產生N個亂數,由小到大排序後顯示


import java.util.Arrays;
import java.util.Scanner;

public class JVD103 {

public static void main(String[] args) {

System .out.print("請輸入欲產生亂數的個數: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int [n];                                   //將輸入的數字做為陣列"長度"
for (int i = 0; i < n ; i++){                          //依序產生亂數放入陣列
a[i] = (int) ((Math.random()*999)+1);
}
Arrays.sort(a);                                         //排序陣列內的數
for (int i = 0; i < n ; i++){
System.out.print(a[i] + "\t");
}
}
}