1、import java.math.BigInteger;import java.util.Hashtable;public class Main private static final int SIZE = 21;private int countArray = new int10; / 个数列表private int countSumArray = new int10; / 个数总数private BigInteger sumArray = new BigInteger10;/ 值总数private int offset = 0;/ 浮标/* 设置当前浮标对应的个数,个数的总数,值总数*
2、param num* 个数*/private void setValue(int num) countArrayoffset = num;if (offset = 0) countSumArrayoffset = num;sumArrayoffset = p(9 - offset).multiply(n(num); else countSumArrayoffset = countSumArrayoffset - 1 + num;sumArrayoffset = sumArrayoffset - 1.add(p(9 - offset).multiply(n(num);/* 检验当前数据是否匹配*
3、 return*/private boolean checkPersentArray() BigInteger minVal = sumArrayoffset;/ 当前已存在值BigInteger maxVal = sumArrayoffset.add(p(9 - offset).multiply(n(SIZE - countSumArrayoffset);/ 当前已存在值+可能存在的最大值/ 最小值匹配if (minVpareTo(MAX) 0) return false;/ 最大值匹配if (maxVpareTo(MIN) 0 ? minVal.toString() : MIN.toStr
4、ing();String maxStr = maxVpareTo(MAX) 0) offset-; else return true;if (offset 0) setValue(countArrayoffset - 1);return false; else return true;/* 测试程序* param startValue* 测试匹配数中包含 9的个数* param startTime* 程序启动时间*/private void test(int startValue, long startTime) / 设置 9的个数offset = 0;setValue(startValue)
5、;while (true) if (checkPersentArray() / 检查当前提交数据是否匹配/ 匹配且总数正好为 SIZE的位数,那么就是求解的值if (countSumArrayoffset = SIZE) success();/ 总数不为 SIZE,且当前值不在第 10位(即不等于 0)if (offset != 9) next();continue;/ 总数不为 SIZE,且当前值在第 10位。if (back() break; else if (back() break;System.out.println(Thread.currentThread() + “ End,Sp
6、end time “ + (System.currentTimeMillis() - startTime) / 1000 + “s“);/* 主函数*/public static void main(String args) final long startTime = System.currentTimeMillis();int s = SIZE 9 ? 9 : SIZE;for (int i = 0; i ht = new Hashtable();static int s = SIZE 10 ? 10 : SIZE;for (int i = 0; i = s; i+) ht.put(“n_“ + i, new BigInteger(String.valueOf(i);for (int i = 0; i = 10; i+) ht.put(“p_“ + i, new BigInteger(String.valueOf(i).pow(SIZE);MIN = n(10).pow(SIZE - 1);MAX = n(10).pow(SIZE).subtract(n(1);