小编mig*_*ios的帖子

确定可能的项目组的算法

我正试图这样做,它正在吞噬我.我知道这不复杂.我有很多项目,这个数字可以等于或大于3.然后我需要确定完成总计的项目组的可能组合.唯一的限制是团体应该有三件或更多件,不超过(但包括)七件.

例如:

如果我有7个项目,那么我可以拥有这些可能的组:

  • 1组7项.
  • 1组4项和1组3项.

如果我有12个项目,我可以拥有这些可能的组:

  • 4组3项.
  • 3组4项.
  • 2组6项.
  • 1组7项+ 1组5项.
  • 2组3组和1组6项.
  • 1组3组,1组4组和1组五项.
  • ...

我考虑了递归并开始实现算法.这显然不起作用.我吮吸递归.很多.

//Instance Fields
public List<ArrayList<String>> options;

//Method that will generate the options. The different options are 
//stored in a list of "option". An individual option will store a list of
//strings with the individual groups.
public void generateOptions(int items, ArrayList<String> currentOption){

    //If the current option is null, then create a new option.
    if(currentOption == null){
        currentOption = new ArrayList<String>();
    }
    if(items < 3){
        //If the number …
Run Code Online (Sandbox Code Playgroud)

java algorithm

9
推荐指数
1
解决办法
1592
查看次数

在Ruby中加入哈希数组

我正在尝试使用公共密钥加入ruby中的多个哈希数组.例如:

country_info = [
  {country_id: "US", country_desc: "United States"}, 
  {country_id: "AU", country_desc: "Australia"}
]
country_stats = [
  {country_id:"US", pageviews: 150},
  {country_id:"AU", pageviews: 200}
]

i_want = [
  {country_id: "US", country_desc: "United States", pageviews:150}, 
  {country_id: "AU", country_desc: "Australia", pageviews:200}
]
Run Code Online (Sandbox Code Playgroud)

这类似于Javascript中protovis的pv.nest功能.请参阅:http://protovis-js.googlecode.com/svn/trunk/jsdoc/symbols/pv.Nest.html

我怎么能在Ruby中做到这一点?

ruby arrays

4
推荐指数
1
解决办法
1734
查看次数

标签 统计

algorithm ×1

arrays ×1

java ×1

ruby ×1