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
看看有问题没有?
相关阅读
这道题就是用Dijkstra 吗?给大家推荐个网站,interviewstreet.com20个包子求祝福啊,LG的opt赶快的阿(外加问个问题)求助:关于国内position应该要多少钱Happy holiday and for help: H-1B RENEW成功守夜留名求VSC加急电话anyone is interested sharing brainbench?找2012暑假EE intern, 茫然中求建议在EAD卡上的开始日期之前工作可以吗?请问OPT到H1B的问题都是给offer, 为什么差别也这么大呢?social 和 cloud 的公司怎么排?2012H1B is over?Does it mean that I can not be transfered to US from China?请推荐distributed system/map reduce方面的reading/booksAmazon is looking for SDET and QA engineers请教问题:gps和google maps背后的算法H1-B tranfer 的一些问题【求教理工专业如何找金融类工作】Offer选择,纽约or 湾区, 求指点!