题解 | #神奇的口袋#
神奇的口袋
https://www.nowcoder.com/practice/9aaea0b82623466a8b29a9f1a00b5d35
#include<iostream> #include<string> #include<map> using namespace std; int item[25]; int res; //用于表示方式的个数 bool isused[25]; int n; void DFS(int curwei, int position) { //curwei表示当前已有重量 for (int i = position; i < n; i++) { //遍历其所有邻居 if (curwei + item[i] > 40 || isused[i] == true) { continue; } isused[i] = true; //表示拿起木棍 if (curwei + item[i] < 40) { DFS(curwei + item[i], i + 1); //继续递归 } else if (curwei + item[i] == 40) { res++; } isused[i] = false; } } int main() { cin >> n; for(int i=0;i<n;i++) { cin >> item[i]; isused[i] = false; } DFS(0, 0); cout << res; }