吸血鬼数字java算法

来源:互联网 发布:mac远程桌面连接软件 编辑:程序博客网 时间:2024/05/20 00:50
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class Test1 {private static int[] A;private static Map<Integer, List<Integer>> LIST = new HashMap<Integer, List<Integer>>();public static void main(String[] args) {// TODO Auto-generated method stubint i=1000;String temp = "";int[] inputArr = new int[4];while(i<10000){A = new int[4];temp = i + "";//将当前四位转化为字符串/** * 将当前四位数放入一个长度为4的数组里 */for (int j=0; j<inputArr.length; j++){inputArr[j] = Integer.parseInt(temp.substring(j, j+1));}/** *  */findXixuegui(0, inputArr, i);i++;}}/** * 判断方法为计算当前四位数字的所有排列,然后调用putLeech方法进行吸血鬼数字判断 * 如果相等则判定为吸血鬼数字,并将该数字记入LIST里 * @param n 从n位开始选择数字做排列 * @param B B为剩余可选数字的组合,其中标记为-1的数字为已使用过的 * @param no no为当前排列数N */public static void findXixuegui(int n, int[] B, int no){for (int i=0; i<B.length; i++){int temp = B[i];if (-1 != temp){A[n] = temp;int[] C = new int[B.length];for (int j=0; j<B.length; j++){C[j] = B[j];};C[i] = -1;if (B.length <= n + 1){putLeech(A, no);}else{findXixuegui(n+1, C, no);}}}}/** * 判断当前四位数是不是吸血鬼数据,如果是的话将数字放入静态变量LIST里 * 按每个排列(排列数记为N)截取前两位与后两位组成两个2位数a、b,再以此进行a*b ?= N * @param K * @param no */public static void putLeech(int[] K, int no){StringBuffer tempBuf = new StringBuffer();for(int i=0; i<K.length; i++){tempBuf.append(K[i]);}String temp = tempBuf.toString();int a = Integer.parseInt(temp.substring(0, 2));int b = Integer.parseInt(temp.substring(2, 4));if (a*b == no){List<Integer> li = LIST.get(no);if (null != li && li.contains(a) && li.contains(b)){return;}List<Integer> te = new ArrayList<Integer>();te.add(a);te.add(b);LIST.put(no, te);System.out.println(no + "=" + a + " * " + b);}}}

0 0
原创粉丝点击