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");
}
}
}

2012年9月30日 星期日

顯示系統時間 -JAVA入門練習

用不同的時間格式,顯示電腦的系統時間


import java.text.DateFormat;
import java.util.Date;

public class JVD102 {

public static void main(String[] args) {
// TODO 自動產生的方法 Stub
Date d = new Date();
DateFormat f1 = DateFormat.getDateTimeInstance(3, 3);
DateFormat f2 = DateFormat.getDateTimeInstance(2, 2);
DateFormat f3 = DateFormat.getDateTimeInstance(1, 1);
DateFormat f = DateFormat.getDateTimeInstance(0, 0);
System.out.println(f1.format(d));
System.out.println(f2.format(d));
System.out.println(f3.format(d));
System.out.println(f.format(d));

}

}
============================================================
比較簡潔的方式 上面的DateFormat()中的參數有遞減的特點,所以可以使用迴圈

import java.text.DateFormat;
import java.util.Date;

public class JVD102_1 {

public static void main(String[] args) {

Date d = new Date();
for (int i=3; i>=0;i--){
System.out.println(DateFormat.getDateTimeInstance(i, i).format(d));
}
}

}

2012年9月29日 星期六

樂透彩模擬程式 JAVA入門練習


1.每次執行程式都會亂數產生6個號碼,和1個特別號
2.號碼不能重複出現
3.號碼在1 - 42之間
4.以二位數顯示 01 - 42

做法一

public class JVD101 {
public static void main(String[] args) {
//1.宣告陣列
int []ar = new int[7];
//2.產生亂數 檢查是否重複 重複則重新產生
for (int i=0;i<7;i++){
int a = (int) (Math.random()*42)+1;

while (a == ar[i]) {      
a = (int) (Math.random() * 42) + 1;
}
ar[i] = a;
//3.依照二位數格式輸出結果
if (i<6){
System.out.print("第"+(i+1)+"個號碼:  ");
}
else{
System.out.print("特別號:  ");
}
if (ar[i]<10){
System.out.println("0"+ar[i]);
}
else{
System.out.println(ar[i]);
}
}
}
===============================================================
做法二 使用DecimalFormat定義輸出格式


import java.text.DecimalFormat;

public class JVD101_1 {
public static void main(String[] args) {

int[] ar = new int[7];
DecimalFormat f = new DecimalFormat("00");

for (int i = 0; i < 7; i++) {
int a = (int) (Math.random() * 42) + 1;
while (a == ar[i]) {
a = (int) (Math.random() * 42) + 1;
}
ar[i] = a;

if (i < 6) {
System.out.println("第" + (i + 1) + "個號碼:  "+f.format(ar[i]));

} else {
System.out.println("特別號:  "+f.format(ar[i]));
}

}

}

}