在网上浏览的时候无意中看到了下面这个题:
把1到9,9个数字放入下面等式,使等式成立。
_ * _ _ = _ _ _ = _ * _ _
作为一名RD当然会想怎么用代码来实现一下,对于老司机而言语言已经无所谓了,这次使用的是Java来实现,对于一个限制范围的问题,本着尽量简化代码编写复杂度,首先采用了递归的实现方式
public static Map<Integer, List<Integer>> range;
public static int c;
public static void main(String[] args) {
range = new HashMap<Integer, List<Integer>>() {
{
put(0, Arrays.asList(2, 3, 4, 6, 7, 8));// 一位数不能是1和5,假定了前面的一位数小于后面的一位数所以9不出现
put(1, Arrays.asList(2, 3, 4, 5, 6, 7, 8, 9));
put(2, Arrays.asList(2, 3, 4, 6, 7, 8, 9));// 两位数个位数不能是1和5
put(3, Arrays.asList(1, 2, 3, 4, 5,6,7));//最大的9*87=783,所以三位数百位不可能是8和9
put(4, Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
put(5, Arrays.asList(1, 2, 3, 4, 6, 7, 8, 9));// 三位数个位数不能是5
put(6, Arrays.asList(3, 4, 6, 7, 8, 9));// 一位数不能是1和5,假定了前面的一位数小于后面的一位数所以2不出现
put(7, Arrays.asList(2, 3, 4, 5, 6, 7, 8, 9));
put(8, Arrays.asList(2, 3, 4, 6, 7, 8, 9));// 两位数个位数不能是1和5
}
};
List<Integer> originalList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
combination(originalList, new ArrayList<Integer>());
System.out.println(c);
}
public static void combination(List<Integer> unusedList, List<Integer> usedList) {
int unusedListCount = unusedList.size();
if (unusedListCount == 0) {
if (isLegal(usedList)) {
System.out.println(usedList.toString());
}
return;
}
for (Integer i : range.get(usedList.size())) {
if (unusedList.contains(i)) {
List<Integer> tmpUnusedList = new ArrayList<Integer>();
tmpUnusedList.addAll(unusedList);
List<Integer> tmpUsedList = new ArrayList<Integer>();
tmpUsedList.addAll(usedList);
tmpUnusedList.remove(i);
tmpUsedList.add(i);
combination(tmpUnusedList, tmpUsedList);
} else {
continue;
}
c++;
}
}
public static boolean isLegal(List<Integer> list) {
int product = list.get(3) * 100 + list.get(4) * 10 + list.get(5);
// list.get(0) < list.get(6) 判断是为了去重,我们假定左边是相对小的一位数字
if (list.get(0) * (list.get(1) * 10 + list.get(2)) == product
&& list.get(6) * (list.get(7) * 10 + list.get(8)) == product && list.get(0) < list.get(6)) {
return true;
}
return false;
}
不过这个递归的实现算法复杂度确实非常差强人意,于是有了另一个实现思路:
public static void main(String[] args) {
Map<Integer, List<Integer>> calcMap = new HashMap<Integer, List<Integer>>();
for (int i = 2; i < 10; i++) {
if (i == 5) {
continue;
}
for (int j = 23; j < 99; j++) {
int singleDigit = j % 10;
if (singleDigit == 0 || singleDigit == 1 || singleDigit == 5) {
continue;
}
int product = i * j;
if (product < 123) {
continue;
}
if (calcMap.containsKey(product)) {
List<Integer> tmpList = new ArrayList<Integer>();
tmpList.addAll(calcMap.get(product));
tmpList.add(i);
tmpList.add(j);
if (isLegal(tmpList)) {
System.out.println(tmpList.toString());
}
} else {
List<Integer> tmpList = Arrays.asList(i, j, product);
if (isLegal(tmpList)) {
calcMap.put(product, tmpList);
}
}
}
}
}
public static boolean isLegal(List<Integer> list) {
Set<Integer> usedSet = new HashSet<Integer>();
List<Integer> splitedList = spliteNumbers(list);
int count = splitedList.size();
usedSet.addAll(splitedList);
if (!usedSet.contains(0) && usedSet.size() == count) {
return true;
}
return false;
}
public static List<Integer> spliteNumbers(List<Integer> list) {
List<Integer> tmpList = new ArrayList<Integer>();
for (Integer i : list) {
while (i > 0) {
tmpList.add(i % 10);
i = i / 10;
}
}
return tmpList;
}