对N各集合中的任意元素进行排列组合问题
2015-10-24 21:40
429 查看
小李去市场买菜,有蔬菜(茄子、黄瓜、大白菜...等k中素菜),和肉类(牛肉,羊肉,鸡肉...等m种荤菜),及点心(麻饼,桃酥,枣花...等n中点心),现在老婆要求每天一荤一素一点心 并且每天的样式要尽可能不重复,这样子有多少中买菜的方案。
问题转化为排列组合问题就是,已知存在N个集合S1、S2.....Sn,每个集合各有m1、m2......mn各元素。依次有序从各个集合当中任意取一个元素,生成集合取样的全排列。
使用递归算法进行实现:
问题转化为排列组合问题就是,已知存在N个集合S1、S2.....Sn,每个集合各有m1、m2......mn各元素。依次有序从各个集合当中任意取一个元素,生成集合取样的全排列。
使用递归算法进行实现:
<span style="font-size:18px;">package optmize; import java.util.ArrayList; import java.util.List; public class Permutate { /**保存生成的排列组合内容**/ public List<String> Permutation = new ArrayList<String>(); /** * 递归的方式计算排列组合 * @param list 传入list.size()个集合 * @param preStr 上一步递归中生成的排列组合 * @return */ public void permutation( List<List<String>> list,String preStr) { int size = list.size(); if(1==size){ for(int i=0; i<list.get(0).size(); i++) { Permutation.add(preStr + list.get(0).get(i)); } } else{ List<String> permu = new ArrayList<String>(list.get(0)); List<List<String>> now = new ArrayList<List<String>>(list); now.remove(0); for(int i=0; i<permu.size(); i++){ permutation(now, preStr +permu.get(i)); } } } public static void main(String[] args) throws Exception { List<List<String>> list = new ArrayList<List<String>>(); List<String> SET1 = new ArrayList<String>(); SET1.add("茄子"); SET1.add("大白菜"); List<String> SET2 = new ArrayList<String>(); SET2.add("牛肉"); SET2.add("羊肉"); List<String> SET3 = new ArrayList<String>(); SET3.add("桃酥"); SET3.add("麻饼"); list.add(SET1); list.add(SET2); list.add(SET3); Permutate permutate = new Permutate(); permutate.permutation(list,""); System.out.println(permutate.Permutation); } }</span>
相关文章推荐
- 常用优化sql语句执行效率的方法
- Spring学习笔记(一)
- light1005 - Rooks【排列组合】
- LeetCode54——Spiral Matrix
- 1.linux运维之rhel7系统安装(详细步骤)
- Keil uVision 4 路径设置修改不了。
- HTTP解析
- 夺命雷公狗---无限级分类NO5
- 系统利益相关者描述案例
- 三种UFT打开程序的方法
- C++设计模式之策略模式
- Hadoop集群搭建
- java 命令模式详解,敢不敢稍微不同一点
- 2015年10月24日作业
- Flask-Bootstrap集成Twitter Bootstrap
- 6、10月23日
- 演讲稿如何结尾
- 《大道至简》第四章读后感
- 课堂讨论记录
- 函数的返回值value的二进制位模式从左到右翻转后的值