大家帮帮忙,我想写一个排列组合的程序,现在组合要是组合问题没有想明白。
排列:现组合,再把各个组合数进行全排列。 import java.util.*;
public class AllSort_2{
static int count = 0;
static char[] buf = {'1', '2', '3', '4','5','6','7'};
static ArrayList<String> list = new ArrayList<String>();
public static void main(String[] args) {
select(buf, list, 7); // 组合
for(String str : list){
char[] temp = str.toCharArray();
perm(temp,0,temp.length-1); //排列
}
System.out.println("排列总数: "+ count);
System.out.print(new String(buf));
}
// 组合(方法还用问题)
public static void select(char[] source, ArrayList<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]);
}
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;
}
}
}
}
排列:现组合,再把各个组合数进行全排列。 import java.util.*;
public class AllSort_2{
static int count = 0;
static char[] buf = {'1', '2', '3', '4','5','6','7'};
static ArrayList<String> list = new ArrayList<String>();
public static void main(String[] args) {
select(buf, list, 7); // 组合
for(String str : list){
char[] temp = str.toCharArray();
perm(temp,0,temp.length-1); //排列
}
System.out.println("排列总数: "+ count);
System.out.print(new String(buf));
}
// 组合(方法还用问题)
public static void select(char[] source, ArrayList<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]);
}
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;
}
}
}
}
http://yiding-he.javaeye.com/blog/58007