吸血鬼數字演算法

來源:互聯網
上載者:User

標籤:語句   else   contain   str   number   stat   ret   solution   for   

這個演算法很巧妙,一定要留下:

class Eg0410_Vampire {    public static void main(String[] args) {        String[] ar_str1, ar_str2;        int sum = 0;        for (int i = 10; i < 100; i++) {            for (int j = i + 1; j < 100; j++) {                int i_val = i * j;                if (i_val < 1000 || i_val > 9999)                    continue; // 積小於1000或大於9999排除,繼續下一輪環                ar_str1 = String.valueOf(i_val).split("");                ar_str2 = (String.valueOf(i) + String.valueOf(j)).split("");                java.util.Arrays.sort(ar_str1);                java.util.Arrays.sort(ar_str2);                if (java.util.Arrays.equals(ar_str1, ar_str2)) {                    // 排序後比較,為真則找到一組                    sum++;                    System.out.println("第" + sum + "組: " + i + "*" + j + "=" + i_val);                }            }        }        System.out.println("共找到" + sum + "組吸血鬼數");    }}

以下是官方答案,比較麻煩,完全沒有涉及本節中所講的中止迴圈語句:

// : c03:E11_Vampire.java// Solution by Dan Forhanimport java.applet.*;import java.awt.*;public class Vampire extends Applet {    private int num1, num2, product;    private int[] startDigit = new int[4];    private int[] productDigit = new int[4];    private int count = 0;    private int vampCount = 0;    private int x, y;    public void paint(Graphics g) {        g.drawString("Vampire Numbers", 10, 20);        g.drawLine(10, 22, 150, 22);        // Positioning for output to applet:        int column = 10, row = 35;        for (num1 = 10; num1 <= 99; num1++)            for (num2 = 10; num2 <= 99; num2++) {                product = num1 * num2;                startDigit[0] = num1 / 10;                startDigit[1] = num1 % 10;                startDigit[2] = num2 / 10;                startDigit[3] = num2 % 10;                productDigit[0] = product / 1000;                productDigit[1] = (product % 1000) / 100;                productDigit[2] = product % 1000 % 100 / 10;                productDigit[3] = product % 1000 % 100 % 10;                count = 0;                for (x = 0; x < 4; x++)                    for (y = 0; y < 4; y++) {                        if (productDigit[x] == startDigit[y]) {                            count++;                            productDigit[x] = -1;                            startDigit[y] = -2;                            if (count == 4) {                                vampCount++;                                int a = (int) Math.random() * 100;                                int b = (int) Math.random() * 100;                                int c = (int) Math.random() * 100;                                if (vampCount < 10) {                                    g.drawString("Vampire number " + vampCount + " is " + num1 + " * " + num2 + " = "                                            + product, column, row);                                    row += 20;                                } else {                                    g.drawString("Vampire number " + vampCount + " is " + num1 + " * " + num2 + " = "                                            + product, column, row);                                    row += 20;                                }                            }                        }                    }            }    }} /// :~

 下面是我寫的,比較隨意,沒有整理,排除法,這個不行,那個不行,剩下的就是行的了。全面地用到了本節的各種中止迴圈及標籤。直接輸出是18個數字,去掉乘數互換的一半,4位的應當是9個數字。

其實沒太明白“吸血鬼數字”是什麼意思,為什麼叫這個名字,為什麼只能是4位元,6位行不行?8位行不行?16位呢?如果這是個隱喻,那麼這個隱喻不是很通用,吸血鬼在西方可能很通用,他們有很多特點,作者也許取其一,西方人一看就知道,但是對於我們不是很瞭解的人,吸血鬼有什麼特點?吸血?面色倉白?尖牙?黑暗?這些都跟這些4位元似乎沒什麼聯絡。也沒有查到合理的解釋。

static void vampire() {        int result = -1;        String strResult = "";        String strj = "", strj2 = "";        String[] arrayj, arrayj2;        String strTotal = "";        String[] arrResult;                int n = 0;        int limit1 = 1000, limit2 = 1000;        ArrayList<String> arrTotal = new ArrayList<String>();        ArrayList<Integer> arrTotalCheck = new ArrayList<Integer>();        System.out.println("吸血鬼數字:");        for (int num1 = 1; num1 < limit1; num1++) {            strj = Integer.toString(num1);            if (Integer.toString(num1).length() < 2)                continue;            l2: for (int num2 = 1; num2 < limit2; num2++) {                strj2 = Integer.toString(num2);                if (Integer.toString(num2).length() < 2)                    continue;                result = num1 * num2;                strTotal = strj + strj2;                strResult = Integer.toString(result);                arrResult = strResult.split("");                arrayj = String.valueOf(num1).split("");                arrayj2 = String.valueOf(num2).split("");                for (String t1 : arrayj) {                    if (!strResult.contains(t1))                        continue l2;                }                for (String t2 : arrayj2) {                    if (!strResult.contains(t2))                        continue l2;                }                for (String tTotal : arrResult) {                    if (!strTotal.contains(tTotal))                        continue l2;                }                arrayj2 = String.valueOf(num2).split("");                if (result >= limit1 * limit2)                    continue;                else if (Integer.toString(result).length() < 4)                    continue;                else if (strResult.length() % 2 != 0)                    continue;                else if (result % 100 == 0)                    continue;                else if (strj.length() != strj2.length())                    continue;                else {                    n++;                    // System.out.println(n + "." + num1 + "*" + num2 + "=" + result + ",");                    if (result == 102510 || result == 105264 || result == 105750 || result==108135                            || result==110758                            ) {                                                                        if(arrTotalCheck.contains(result)) continue;                        arrTotalCheck.add(result);                                                arrTotal.add(n + "." + num1 + "*" + num2 + "=" + result + ",");                                            };                }            }        }                for (String i : arrTotal) {            System.out.println(i);        }        return;    }

根據某百科上的資料可知,有6位的吸血鬼數字,那麼有8位的嗎?10位的?12位的?理論上應該有,偶數位即可。

那麼5775是不是?

4位元的吸血鬼數有7個:1260, 1395, 1435, 1530, 1827, 2187, 68806位元的吸血鬼數字有141個:102510, 104260, 105210, 105264, 105750, 108135, 110758, 115672, 116725, 117067, 118440, 123354, 124483, 125248, 125433, 125460, 126027, 126846, 129640, 129775, 131242, 132430, 133245, 134725, 135828, 135837, 136525, 136948, 140350, 145314, 146137, 146952, 152608, 152685, 153436, 156240, 156289, 156915, 162976, 163944, 172822, 173250, 174370, 175329, 180225, 180297, 182250, 182650, 186624, 190260, 192150, 193257, 193945, 197725, 201852, 205785, 211896, 213466, 215860, 216733, 217638, 218488, 226498, 226872, 229648, 233896, 241564, 245182, 251896, 253750, 254740, 260338, 262984, 263074, 284598, 284760, 286416, 296320, 304717, 312475, 312975, 315594, 319059, 319536, 326452, 329346, 329656, 336550, 336960, 338296, 341653, 346968, 361989, 362992, 365638, 368550, 369189, 371893, 378418, 378450, 384912, 386415, 392566, 404968, 414895, 416650, 416988, 428980, 429664, 447916, 456840, 458640, 475380, 486720, 489159, 489955, 498550, 516879, 529672, 536539, 538650, 559188, 567648, 568750, 629680, 638950, 673920, 729688, 736695, 738468, 769792, 789250, 789525, 792585, 794088, 809919, 809964, 815958, 829696, 841995, 939658其中125460比較特殊,因為125640=204*615且125640=246*510.偽吸血鬼數和一般吸血鬼數不同之處在於其尖牙不強制是n/2個位的數,故偽吸血鬼數的位元可以是奇數。2002年Carlos Rivera定義了質吸血鬼數:尖牙是質因子的吸血鬼數,例如117067, 124483, 146137, 371893, 536539。 根據“尖牙不強制是n/2個位的數”來推斷,尖牙指的是兩個乘數,但是,為什嗎?哪裡尖了?莫非是說對稱?那也太牽強了。

吸血鬼數字演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.