求1000-9999之间的吸血鬼数字(穷举法Java实现)

当初学Java时做的。

代码:

展开
 1 public class XiXueGuiNum {
 2  
 3      /**
 4       * @param args
 5       */
 6      public static void main(String[] args) {
 7          // TODO Auto-generated method stub
 8          int num[] = new int[4];
 9          for(int i = 1000;i < 10000;++i){
10              if(i%100 == 0)
11                  continue;
12              convertToSingleValue(i,num);
13              
14              for(int a = 0; a < num.length;++a)
15              {
16                  for(int b = 0;b < num.length;++b)
17                  {
18                      if(a == b)
19                          continue;
20                      for(int c = 0;c < num.length;++c)
21                      {
22                          if(c == a || c == b)
23                              continue;
24                          for(int d = 0;d < num.length;++d)
25                          {
26                              if(d == a || d == b || d == c)
27                                  continue;
28                              if((num[a]*10 + num[b]) * (num[c]*10 + num[d]) == i)
29                                  System.out.println(i + "=" + (num[a]*10 + num[b]) + "*" + (num[c]*10 + num[d]));
30                          }
31                      }
32                  }
33              }
34              
35              if((num[0] * 10 + num[1]) * (num[2] * 10 + num[3]) == i)
36              {
37                  System.out.println(i+"=" + num[0]*10 + num[1] + "*" + num[2]*10 + num[3]);
38              }
39          }
40      }
41      public static void convertToSingleValue(int value,int[] num)
42      {
43          int temp = value;
44          int quotient = 0;
45          int remainder = 0;
46          int[] times = {1000,100,10,1};
47          for(int i = 0;i < num.length;++i)
48          {
49              quotient = temp/times[i];
50              remainder = temp % times[i];
51              temp = remainder;
52              num[i] = quotient;
53          }
54      }
55  }

 

 

posted @ 2013-04-26 20:17  Jinks  阅读(592)  评论(0编辑  收藏  举报