s*n
2 楼
for number of cards from 1 to 为。, do
for each collection of cards,
if cards number < n,
foreach the rest card c of this collection,
sum = c + sum(collection)
if sum < n, add c union collection into a new collection,
if sum = n, print c union collection,
end foreach
remove the collection from the collection of the collection
end foreach
end for
【在 P*******b 的大作中提到】
: 从一副牌中选n张,使其和为。
: 打印所有结果。
: 要求不能用递归。
: 谁给讲讲思路吧。谢谢
for each collection of cards,
if cards number < n,
foreach the rest card c of this collection,
sum = c + sum(collection)
if sum < n, add c union collection into a new collection,
if sum = n, print c union collection,
end foreach
remove the collection from the collection of the collection
end foreach
end for
【在 P*******b 的大作中提到】
: 从一副牌中选n张,使其和为。
: 打印所有结果。
: 要求不能用递归。
: 谁给讲讲思路吧。谢谢
P*b
3 楼
谢谢回复。再问一下。这里for each collection of cards, 这些collection这么造出
来的?
【在 s*****n 的大作中提到】
: for number of cards from 1 to 为。, do
: for each collection of cards,
: if cards number < n,
: foreach the rest card c of this collection,
: sum = c + sum(collection)
: if sum < n, add c union collection into a new collection,
: if sum = n, print c union collection,
: end foreach
: remove the collection from the collection of the collection
: end foreach
来的?
【在 s*****n 的大作中提到】
: for number of cards from 1 to 为。, do
: for each collection of cards,
: if cards number < n,
: foreach the rest card c of this collection,
: sum = c + sum(collection)
: if sum < n, add c union collection into a new collection,
: if sum = n, print c union collection,
: end foreach
: remove the collection from the collection of the collection
: end foreach
P*l
8 楼
这是那个“数组里两个数和为给定值”问题的扩展版。
觉得枚举每个组合就挺好。比如,52张牌选三张的组合是
1, 1, 1
1, 1, 2
1, 1, 3
。。。
1, 1, 13
1, 2, 2
1, 2, 3
。。。
11, 13, 13
12, 12, 12
12, 12, 13
12, 13, 13
13, 13, 13
如果要选4张牌,使其和为28,就可以通过前三张算出第四张,然后检查是否符合要求。
代码:
http://code.google.com/p/sureinterview/source/browse/test/test1/CombinationTest.java#120
public void testNumComb2() {
// list all combinations of c(7,3)
int suit = 4;
int rank = 13;
int takeN = 3; //4-1=3。
int totalNum = 28;
List numList = new ArrayList();
for (int i = 1; i <= rank; i++) {
for (int j = 0; j < suit; j++) {
numList.add(i);
}
}
Combination comb = new CombinationImpl(numList,
takeN);
for (List cb : comb) {
// System.out.println(StringUtils.join(cb.toArray(), ", "));
int sum = 0;
for (Integer num : cb) {
sum += num;
}
int lastNum = totalNum - sum;
int tk1 = cb.get(takeN - 1);
//检查是否符合要求
if (lastNum <= rank && (lastNum > tk1 || //
((takeN < suit || tk1 == cb.get(takeN - suit)) &&
lastNum == tk1))) {
System.out.print(StringUtils.join(cb.toArray(), ", "));
System.out.println(", " + lastNum);
}
}
}
枚举带重复的组合的代码:
http://code.google.com/p/sureinterview/source/browse/src/lib/combination/CombinationImpl.java#158
看看有问题没有?
觉得枚举每个组合就挺好。比如,52张牌选三张的组合是
1, 1, 1
1, 1, 2
1, 1, 3
。。。
1, 1, 13
1, 2, 2
1, 2, 3
。。。
11, 13, 13
12, 12, 12
12, 12, 13
12, 13, 13
13, 13, 13
如果要选4张牌,使其和为28,就可以通过前三张算出第四张,然后检查是否符合要求。
代码:
http://code.google.com/p/sureinterview/source/browse/test/test1/CombinationTest.java#120
public void testNumComb2() {
// list all combinations of c(7,3)
int suit = 4;
int rank = 13;
int takeN = 3; //4-1=3。
int totalNum = 28;
List
for (int i = 1; i <= rank; i++) {
for (int j = 0; j < suit; j++) {
numList.add(i);
}
}
Combination
takeN);
for (List
// System.out.println(StringUtils.join(cb.toArray(), ", "));
int sum = 0;
for (Integer num : cb) {
sum += num;
}
int lastNum = totalNum - sum;
int tk1 = cb.get(takeN - 1);
//检查是否符合要求
if (lastNum <= rank && (lastNum > tk1 || //
((takeN < suit || tk1 == cb.get(takeN - suit)) &&
lastNum == tk1))) {
System.out.print(StringUtils.join(cb.toArray(), ", "));
System.out.println(", " + lastNum);
}
}
}
枚举带重复的组合的代码:
http://code.google.com/p/sureinterview/source/browse/src/lib/combination/CombinationImpl.java#158
看看有问题没有?
相关阅读
女manager不给办绿卡实习的时候怎样表现比较好?亚麻内推请教下怎么从南湾到twitter去?老中技术“高手”,决战紫禁之巅 (转载)求助:New Grad该如何Negotiate offer?NEW GRAD干多久跳槽比较合适?leetcode最近发飙啊,题目更新真快OPT 申请求助 -- I20忘记签名fire和layoff的区别那道H2O的题帖面筋,大小公司都有。面试求助请教:奇怪的overlap of OPT and CPT美国抓华人间谍对大家找工作有影响么?全被冻住后, 准备水cs master没活路了。。。。。对手是各种滕校master和各种cs博士十个人的小公司,会不会加班很严重?Re: 我也提供个Bloomberg refer我觉得现在找份码农的工作, 太难了