標籤:print net log ret 技術 false [] pos color
第三題
三羊獻瑞
觀察下面的加法算式:
其中,相同的漢字代表相同的數字,不同的漢字代表不同的數字。
請你填寫“三羊獻瑞”所代表的4位元字(答案唯一),不要填寫任何多餘內容。
答案這個題目完全可以使用暴力破解模型,就是從0~9中選取8個數,進行全排列,然後看情況是否符合
但是我在這裡遇到了一個問題 就是暴力破解模型失誤了,怎麼說,需要選取8個數 我排了10個數,然後資料得到的結果錯誤了
後來一想我沒有錯 畢竟答案唯一:1085 美滋滋
import java.util.Stack;//思想得出全排列 然後選取前八個資料為資料排列 進行測試//如果資料複習排列規則 就輸出//但是這個地方 我錯誤點在於將其10個全排列 //然後選取其中的前八個作為資料public class Test3A { private static int count = 0; private static int sum = 0; public static void main(String[] args) { Stack<Integer> stack = new Stack<Integer>(); for (int i = 0; i <= 9; i++) { stack.push(i); fun(stack); stack.pop(); } System.out.println(count); System.out.println(sum); } private static void fun(Stack<Integer> stack) { for (int j = 0; j <= 9; j++) { if (stack.size() == 10) { int a = stack.get(0); int b = stack.get(1); int c = stack.get(2); int d = stack.get(3); int e = stack.get(4); int f = stack.get(5); int g = stack.get(6); int h = stack.get(7); if (check(a, b, c, d, e, f, g, h)) { ++count; System.out.printf("%d %d %d %d %d %d %d %d", a, b, c, d, e, f, g, h); System.out.println(); } return; } if (!stack.contains(j)) { stack.push(j); fun(stack); stack.pop(); } } } private static boolean check(int... args) { int ABCD = args[0] * 1000 + args[1] * 100 + args[2] * 10 + args[3]; int EFGB = args[4] * 1000 + args[5] * 100 + args[6] * 10 + args[1]; int EFCBH = args[4] * 10000 + args[5] * 1000 + args[2] * 100 + args[1] * 10 + args[7]; if (ABCD + EFGB == EFCBH && args[4] != 0) { return true; } return false; }}
第六屆藍橋杯java b組第三題