求一个小程序算法-- 组合问题的

dearxiaoxin 2008-02-28 01:02:33
编程题:让用户输入任意个字符,然后打印出输入字符的所有可能组合
例如:输入a,b,c
则打印出如下结果:
abc
acb
bac
bca
cab
cba

这类程序属于什么算法问题,通用的思路是怎样的,谢谢!
...全文
179 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
dearxiaoxin 2008-03-30
  • 打赏
  • 举报
回复
谢谢!
sunnylyy 2008-02-29
  • 打赏
  • 举报
回复
楼主问的字符串的全排列,九楼的朋友写的是子集,数量不一样,一个是n!,一个是2的N次幂。

11楼的答案是对的,不过顺序不是严格楼主想要的,可以加个排序的过程。


关于全排列,有它的字典算法,具体内容上网google就行了。

zapdos 2008-02-29
  • 打赏
  • 举报
回复


public class test{
String[] list;
StringBuffer sb = new StringBuffer();
int start;
test(String in){
list = in.split(",");
printList();
System.out.print(sb);
}
void replaceStr(int c1,int c2){
String temp = list[c1];
list[c1] = list[c2];
list[c2] = temp;
}
void printList(){
if(start==list.length-1)
for(int i=0;i<list.length;i++)
sb.append(list[i]+(i==list.length-1?"\n":","));
else
for(int i=start;i<list.length;i++){
replaceStr(start++,i);
printList();
replaceStr(i,--start);
}
}
public static void main(String args[]){
new test("1,2,3,4,5,6");
}
}

zapdos 2008-02-29
  • 打赏
  • 举报
回复
这样也可以

public class test{
String[] list;
StringBuffer sb = new StringBuffer();
int start;
test(String in){
list = in.split(",");
printList();
System.out.print(sb);
}
void replaceStr(int c1,int c2){
String temp = list[c1];
list[c1] = list[c2];
list[c2] = temp;
}
void printList(){
if(start==list.length-1)
for(int i=0;i<list.length;i++)
sb.append(list[i]+(i==list.length-1?"\n":","));
else
for(int i=start;i<list.length;i++){
replaceStr(start++,i);
printList();
replaceStr(i,--start);
}
}
public static void main(String args[]){
new test("1,2,3,4,5,6");
}
}

Moon 2008-02-28
  • 打赏
  • 举报
回复
采用位运算的思路
我做过一个类似的例子,你可以看一下

/*求
*k元集的
*所有子集划分
*思想:k项集的子集个数为2^k个,从直观上来看就是
*k个比特位上,每一位不是0就是1,
*按照此思路并结合编程语言的位操作,便能产生简洁的算法程序
*/
public Hashtable<ItemSet, ItemSet> getAllSub() {
//初始化结果集合,用于存放结果
Hashtable<ItemSet, ItemSet> res = new Hashtable<ItemSet, ItemSet>();
//依次生成每一个子集
int length = items.size();
for (int i = 1; i < (1 << length) - 1; i++) {
//左部项目集合
TreeSet<String> item_left = new TreeSet<String>();
//右部项目集合(备份原来数据)
Vector<String> item_right = new Vector<String>(items);
for (int j = length - 1; j >= 0; j--) {
if ((i & (1 << j)) != 0) {
item_left.add(items.get(j));
item_right.remove(j);
}
}
ItemSet left = new ItemSet(item_left);
ItemSet right = new ItemSet(item_right);
//添加结果
res.put(left, right);
}
return res;
}
ninesea 2008-02-28
  • 打赏
  • 举报
回复
上次某人发的代码 被偶copy了 呵呵

/**
* @param num how many characters in one result
* @param array range of the alphabets
* @return all sequences
*/
public static String[] getAllSequence(int num, String[] array) {
if (num == 1) {
return array;
}
else {
String[] currentArray = getAllSequence(num - 1, array);
String[] newArray = new String[array.length * currentArray.length];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < currentArray.length; j++) {
newArray[i * currentArray.length + j] = array[i] + currentArray[j];
}
}
return newArray;
}
}
dearxiaoxin 2008-02-28
  • 打赏
  • 举报
回复
谁能帮忙写出基本的代码,谢谢
rinehart 2008-02-28
  • 打赏
  • 举报
回复
属于组合数学,有专门的组合算法解决

试试这个,

http://www.merriampark.com/perm.htm
dearxiaoxin 2008-02-28
  • 打赏
  • 举报
回复
等...
foundercy_weng 2008-02-28
  • 打赏
  • 举报
回复
用递归实现吧
dirclstype 2008-02-28
  • 打赏
  • 举报
回复
这个排法啊!做个函数。
dearxiaoxin 2008-02-28
  • 打赏
  • 举报
回复
题目写的很清楚了呀
foundercy_weng 2008-02-28
  • 打赏
  • 举报
回复
用数学的方法解决吧,排列组合是吗,是不是任意输入n个数,然后进行排列组合

62,623

社区成员

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

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