题解 | #神奇的口袋#
神奇的口袋
https://www.nowcoder.com/practice/9aaea0b82623466a8b29a9f1a00b5d35
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> #include <map> #include <queue> using namespace std; int arr[30]; int _count = 0; int n; void select(int i, int weight) { //选到了第i个位置,此时的容量为weight if (weight == 40) { _count++; return; } if (weight > 40 || i >= n) { //重量超过了,或者越界 return; } //可以选择拿第i个物品,或者不拿 select(i + 1, weight + arr[i]); select(i + 1, weight); return; } int main() { while (scanf("%d", &n) != EOF) { for (int i = 0; i < 30; i++) { arr[i] = 0; } for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } select(0, 0); printf("%d\n", _count); } }