题解 | #神奇的口袋#
神奇的口袋
http://www.nowcoder.com/practice/9aaea0b82623466a8b29a9f1a00b5d35
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 20;
int things[MAXN];
void DFS(int step, int n, int pos, int sum, int *ans){//n是物品总数; 预先排序
if(step > n){
return;
}
for(int i=pos; i<n; i++){
sum += things[i];
if(sum > 40){
return;
}else if(sum == 40){
*ans += 1;
}else if(sum < 40){
DFS(step+1, n, i+1, sum, ans);//一路return回去的设计是“搜到即可”,但本题是要计数
}
sum -= things[i];
}
return;
}
int main(){
int n;
while(scanf("%d",&n) != EOF){
for(int i=0; i<n; i++){
scanf("%d",&things[i]);
}
int ans = 0;
sort(things, things+n);
DFS(1,n,0,0,&ans);
printf("%d\n",ans);
}
return 0;
}