第六屆藍橋杯java b組第三題

來源:互聯網
上載者:User

標籤: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組第三題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.