求java程序能输出0-9 a-z A-Z的排列可能,要求8位数到12位的所有可能
展开全部
贴一个代码给你,没写全,能表达意思
package snippet;
import java.util.*;
public class Sort {
static int count = 0;
static char[] buf = { '1', '2', '3', '4' ,'5','6','7','8','9','0','a','b'};
static List<String> list = new ArrayList<String>();
public static void main(String[] args) { select(buf, list, 3);
for (String str : list) {
System.out.println(str);
}
System.out.println("In total: " + count); }
public static void select(char[] source, List<String> arrayList,
int num) {
int l = source.length;
char[] temp = new char[num];
System.arraycopy(source, 0, temp, 0, num);
arrayList.add(new String(temp));
for (int i = num; i < l; i++) {
for (int j = 0; j < num; j++) {
char tempChar = temp[j];
temp[j] = source[i];
arrayList.add(new String(temp));
temp[j] = tempChar;
}
}
}
public static void perm(char[] buf, int start, int end) {
if (start == end) {// 当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可
for (int i = 0; i <= end; i++) {
System.out.print(buf[i]);
}
Sort.count++;
System.out.println();
} else {// 多个字母全排列
for (int i = start; i <= end; i++) {
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;
}
}
}
}
package snippet;
import java.util.*;
public class Sort {
static int count = 0;
static char[] buf = { '1', '2', '3', '4' ,'5','6','7','8','9','0','a','b'};
static List<String> list = new ArrayList<String>();
public static void main(String[] args) { select(buf, list, 3);
for (String str : list) {
System.out.println(str);
}
System.out.println("In total: " + count); }
public static void select(char[] source, List<String> arrayList,
int num) {
int l = source.length;
char[] temp = new char[num];
System.arraycopy(source, 0, temp, 0, num);
arrayList.add(new String(temp));
for (int i = num; i < l; i++) {
for (int j = 0; j < num; j++) {
char tempChar = temp[j];
temp[j] = source[i];
arrayList.add(new String(temp));
temp[j] = tempChar;
}
}
}
public static void perm(char[] buf, int start, int end) {
if (start == end) {// 当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可
for (int i = 0; i <= end; i++) {
System.out.print(buf[i]);
}
Sort.count++;
System.out.println();
} else {// 多个字母全排列
for (int i = start; i <= end; i++) {
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;
}
}
}
}
追问
自己用递归写出了一个算法,但是搞不明白固定位数该怎么办。求解!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询