我绞尽脑汁想做这件事,但它让我筋疲力尽。我知道这并不复杂。我有一些元素,这个数量可以等于或大于三。然后我需要确定将完成总数的项目组的可能组合。唯一的限制是组应该有三个或更多项目,但不超过(但包括)七个项目。
例如:
如果我有 7 个项目,那么我可以有这些可能的组:
如果我有 12 个项目,我可以有这些可能的组:
我想到了递归并开始实现算法。这显然是行不通的。我不擅长递归。很多。
//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 of items is less than three then it doesn't comply with the
//requirements (teams should be more or equal than three.
currentOption.add("1 group of "+items+" items");
options.add(currentOption);
}
else{
//I can make groups of 3,4,5,6 and 7 items.
for(int i = 3;i<=7;i++){
if(items%i == 0){
// If the number of items is divisible per the current number,
// then a possible option could be items/i groups of i items.
// Example: Items = 9. A possible option is 3 groups of 3 items.
currentOption.add(items/i +" groups of "+ i+" items");
options.add(currentOption);
}
else{
// If the number of items - the current number is equal or greater than
// three, then a possible option could be a group of i items
// and then I'll have items-i items to separate in other groups.
if(items - i >=3){
currentOption.add("1 group of "+i+" items");
generateOptions(items-i,currentOption);
}
}
}
}
}
感谢您的帮助!!!
最佳答案
这里有一个算法(用 C++ 表示)来解决这个问题的更一般版本, 对可能出现在每个分区中的加数具有任意上限和下限:
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> Partition;
typedef vector<Partition> Partition_list;
// Count and return all partitions of an integer N using only
// addends between min and max inclusive.
int p(int min, int max, int n, Partition_list &v)
{
if (min > max) return 0;
if (min > n) return 0;
if (min == n) {
Partition vtemp(1,min);
v.push_back(vtemp);
return 1;
}
else {
Partition_list part1,part2;
int p1 = p(min+1,max,n,part1);
int p2 = p(min,max,n-min,part2);
v.insert(v.end(),part1.begin(),part1.end());
for(int i=0; i < p2; i++)
{
part2[i].push_back(min);
}
v.insert(v.end(),part2.begin(),part2.end());
return p1+p2;
}
}
void print_partition(Partition &p)
{
for(int i=0; i < p.size(); i++) {
cout << p[i] << ' ';
}
cout << "\n";
}
void print_partition_list(Partition_list &pl)
{
for(int i = 0; i < pl.size(); i++) {
print_partition(pl[i]);
}
}
int main(int argc, char **argv)
{
Partition_list v_master;
int n = atoi(argv[1]);
int min = atoi(argv[2]);
int max = atoi(argv[3]);
int count = p(min,max,n,v_master);
cout << count << " partitions of " << n << " with min " << min ;
cout << " and max " << max << ":\n" ;
print_partition_list(v_master);
}
和一些示例输出:
$ ./partitions 12 3 7
6 partitions of 12 with min 3 and max 7:
6 6
7 5
4 4 4
5 4 3
6 3 3
3 3 3 3
$ ./partitions 50 10 20
38 partitions of 50 with min 10 and max 20:
17 17 16
18 16 16
18 17 15
19 16 15
20 15 15
18 18 14
19 17 14
20 16 14
19 18 13
20 17 13
19 19 12
20 18 12
13 13 12 12
14 12 12 12
20 19 11
13 13 13 11
14 13 12 11
15 12 12 11
14 14 11 11
15 13 11 11
16 12 11 11
17 11 11 11
20 20 10
14 13 13 10
14 14 12 10
15 13 12 10
16 12 12 10
15 14 11 10
16 13 11 10
17 12 11 10
18 11 11 10
15 15 10 10
16 14 10 10
17 13 10 10
18 12 10 10
19 11 10 10
20 10 10 10
10 10 10 10 10
关于java - 确定可能的项目组的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2129805/
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在编写一个方法,它将在一个类中定义一个实例方法;类似于attr_accessor:classFoocustom_method(:foo)end我通过将custom_method函数添加到Module模块并使用define_method定义方法来实现它,效果很好。但我无法弄清楚如何考虑类(class)的可见性属性。例如,在下面的类中classFoocustom_method(:foo)privatecustom_method(:bar)end第一个生成的方法(foo)必须是公共(public)的,第二个(bar)必须是私有(private)的。我怎么做?或者,如何找到调用我的cust
几个月前,我读了一篇关于rubygem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我