码迷,mamicode.com
首页 > 编程语言 > 详细

Java:thinging in java p154 exercise 10

时间:2015-04-17 13:28:17      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

题目

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 * 60
2 1395 = 15 * 93
3 1435 = 41 * 35
4 1530 = 51 * 30
5 1827 = 87 * 21
6 2187 = 27 * 81
7 6880 = 86 * 80
8 6880 = 80 * 86

 

Java:thinging in java p154 exercise 10

标签:

原文地址:http://www.cnblogs.com/taoxiuxia/p/4434527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!