Java算法編程題系列之 3、猜算式

Java算法編程題系列之 3、猜算式
題目:
看下面的算式:
□□ x □□ = □□ x □□□
它表示:兩個兩位數相乘等於一個兩位數乘以一個三位數。
如果沒有限定條件,這樣的例子很多。
但目前的限定是:這9個方塊,表示1~9的9個數字,不包含0。
該算式中1至9的每個數字出現且只出現一次!
比如:
46 x 79 = 23 x 158
54 x 69 = 27 x 138
54 x 93 = 27 x 186
…..
請編程,輸出所有可能的情況!
注意:
左邊的兩個乘數交換算同一方案,不要重複輸出!
不同方案的輸出順序不重要
題目解答:

import java.util.*;


public class Question3 {
    public static long count = 0;
    public static List<Vector<Character>> filteredNonRedundantResults;
    /* 用於存放全排列的結果  */

    private static boolean isfilter(Vector<Character> result) { 
        /* 按照要求條件進行過濾 判斷 */
        int a = (result.elementAt(0) - '0') * 10 + (result.elementAt(1) - '0');
        int b = (result.elementAt(2) - '0') * 10 + (result.elementAt(3) - '0');
        int c = (result.elementAt(4) - '0') * 10 + (result.elementAt(5) - '0');
        int d = (result.elementAt(6) - '0') * 100 + (result.elementAt(7) - '0') * 10 + (result.elementAt(8) - '0');
        if (a*b == c*d) {
            return true;
        }
        return false;
    }
    public static void print(Vector<Character> vector) {
        /* 格式化輸出結果 */
        System.out.printf("%c%c x %c%c = %c%c x %c%c%c",vector.elementAt(0),vector.elementAt(1),vector.elementAt(2),vector.elementAt(3),vector.elementAt(4),vector.elementAt(5),vector.elementAt(6),vector.elementAt(7),vector.elementAt(8));
    }
    private static void fullPermutation(Vector<Character> sourse, Vector<Character> result) {
        /* 全排列 */
        if (sourse.size() == 0 && isfilter(result)) {
            boolean exit = false;
            for (int i = 0;i < filteredNonRedundantResults.size();i++) {
                int ra = (result.elementAt(0) - '0') * 10 + (result.elementAt(1) - '0');
                int rb = (result.elementAt(2) - '0') * 10 + (result.elementAt(3) - '0');
                int fa = (filteredNonRedundantResults.get(i).elementAt(0) - '0') * 10 + (filteredNonRedundantResults.get(i).elementAt(1) - '0');
                int fb = (filteredNonRedundantResults.get(i).elementAt(2) - '0') * 10 + (filteredNonRedundantResults.get(i).elementAt(3) - '0');
                if (ra==fb && rb==fa) { //去除重複的數據
                    exit = true;
                    break;
                }
            } 
            if (exit==false) {//結果中不存在該組數據,添加進結果中
                filteredNonRedundantResults.add(new Vector<Character> (result));
            }
            return;
        }
        for (int i = 0; i < sourse.size();i++) {
            result.add(sourse.elementAt(i));
            sourse.remove(i);
            fullPermutation(sourse,result);
            sourse.add(i, result.elementAt(result.size() - 1));
            result.remove(result.size() - 1);
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = 9;
        Vector<Character> sourse = new Vector<Character> ();
        Vector<Character> result = new Vector<Character> ();
        for(int i =1 ;i <= n;i++) {
            sourse.add((char)('0' + i));
        }
        Question3.filteredNonRedundantResults = new ArrayList<Vector<Character>> ();
        Question3.fullPermutation(sourse, result);
        for(int i = 0;i < Question3.filteredNonRedundantResults.size();i++) {
            Question3.print(Question3.filteredNonRedundantResults.get(i));
            System.out.println();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章