Java:thinging in java p154 exercise 10

來源:互聯網
上載者:User

標籤:

題目

A vampire number has an even number of digits and is formed by multiplying a pair of numbers containing half the number of digits of the result. The digits are taken from the original number in any order. Pairs of trailing zeroes are not allowed. Examples include: 1260 = 21 * 60, 1827 = 21 * 87, 2187 = 27 * 81. Write a program that finds all the 4-digit vampire numbers. (Suggested by Dan Forhan.)

 1 public class VampireNumber { 2  3     public static void main(String[] args) { 4         int a = 0, b = 0, c = 0, d = 0; 5         int[] m = new int[12]; 6         int[] n = new int[12]; 7  8         for (int i = 1000; i < 9999; i++) { 9             // 分成4個數10             a = i / 1000;11             b = (i - a * 1000) / 100;12             c = (i - a * 1000 - b * 100) / 10;13             d = i % 10;14 15             m[0] = a * 10 + b;16             n[0] = c * 10 + d;17 18             m[1] = a * 10 + b;19             n[1] = d * 10 + c;20 21             m[2] = a * 10 + c;22             n[2] = b * 10 + d;23 24             m[3] = a * 10 + c;25             n[3] = d * 10 + b;26 27             m[4] = a * 10 + d;28             n[4] = b * 10 + c;29 30             m[5] = a * 10 + d;31             n[5] = c * 10 + b;32 33             m[6] = b * 10 + a;34             n[6] = c * 10 + d;35 36             m[7] = b * 10 + a;37             n[7] = d * 10 + c;38 39             m[8] = b * 10 + c;40             n[8] = d * 10 + a;41 42             m[9] = b * 10 + d;43             n[9] = c * 10 + a;44 45             m[10] = c * 10 + a;46             n[10] = d * 10 + b;47 48             m[11] = c * 10 + b;49             n[11] = d * 10 + a;50             for (int j = 0; j < 12; j++) {51                 if (i == m[j] * n[j]) {52                     System.out.println(i + " = " + m[j] + " * " + n[j]);53                 }54             }55 56         }57     }58 }

輸出

1 1260 = 21 * 602 1395 = 15 * 933 1435 = 41 * 354 1530 = 51 * 305 1827 = 87 * 216 2187 = 27 * 817 6880 = 86 * 808 6880 = 80 * 86

 

Java:thinging in java p154 exercise 10

聯繫我們

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