数组完全组合算法

xiaochengfu1 2010-10-14 05:01:33
如题,谁有这个算法的代码?
...全文
105 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaochengfu1 2010-10-15
  • 打赏
  • 举报
回复
xiaochengfu1 2010-10-15
  • 打赏
  • 举报
回复
/**
* 使用createList方法,填充参数列表传递过来的List,默认是Integer,一般是这个类型,你可以修改别的类型
*/
public void createList(int n,List list){
if(n==0){
n=3;
}
for(int i=1;i<=n;i++){
list.add(i);
}
}
/**
* printAll是输出全排列的递归调用方法,list是传入的list,用LinkedList实现,
* 而prefix用于转载以及输出的数据
* length用于记载初始list的长度,用于判断程序结束。
*/
public void printAll(List candidate, String prefix,int length){
if(prefix.length()==length)
System.out.println(prefix);
for (int i = 0; i < candidate.size(); i++) {
List temp = new LinkedList(candidate);
printAll(temp, prefix + temp.remove(i),length);
}
}

/**
* 测试代码
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> list=new ArrayList<Integer>();
Test lap=new Test();
lap.createList(3, list);
lap.printAll(list,"",list.size());
}
看看这个怎么

81,122

社区成员

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

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