求一个 排列组合程序

tanbin_0521 2010-08-03 09:13:18
大家帮帮忙,我想写一个排列组合的程序,现在组合要是组合问题没有想明白。
排列:现组合,再把各个组合数进行全排列。





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;
}
}
}
}



...全文
193 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
e9876 2010-08-04
  • 打赏
  • 举报
回复
不明,能不能举几个例子
例如 原来的排序是什么,你期待输出的排序又是什么
tanbin_0521 2010-08-04
  • 打赏
  • 举报
回复
求一个排列组合的程序啊。帮我改改上面的代码也行。
xbyan_gmail2010 2010-08-04
  • 打赏
  • 举报
回复
what are you doing?
捏造的信仰 2010-08-04
  • 打赏
  • 举报
回复
参考这里《可遍历的全排列》:
http://yiding-he.javaeye.com/blog/58007

62,617

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧