package com.epro;
import java.util.ArrayList;
import java.util.List;
public class digui {
public static void perm(char[] buf, int start, int end) {
List<Integer> list = new ArrayList<Integer>();
if (start == end) {// 当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可(特殊情况)
for (int i = 0; i <= end; i++) {
// System.out.print(buf[i]);
list.add(buf[i] - 48);
}
} else {// 多个字母全排列(普遍情况)
for (int i = start; i <= end; i++) {// (让指针start分别指向每一个数)
char temp = buf[start];// 交换数组第一个元素与后续的元素
buf[start] = buf[i];
buf[i] = temp;
perm(buf, start + 1, end);// 后续元素递归全排列
temp = buf[start];// 将交换后的数组还原
buf[start] = buf[i];
buf[i] = temp;
}
}
if (list.size() != 0) {
if ((list.get(0) * 1000 + list.get(1) * 100 + list.get(2) * 10 + list
.get(3)) * list.get(4) == (list.get(5) * 1000 + list.get(6)
* 100 + list.get(7) * 10 + list.get(8))) {
System.out.println(list.get(0) +""+ list.get(1)+"" + list.get(2)+""
+ list.get(3) + "*" + list.get(4) + "=" + list.get(5)
+ list.get(6) + list.get(7) + list.get(8));
}
}
}
public static void main(String[] args) {
char[] buf = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
perm(buf, 0, buf.length - 1);
// 1738*4=6952
}
}
转载于:https://www.cnblogs.com/jugg-mine/p/3475112.html