//题目要求搜寻所有解,而不是是否可以,所以DFS时,不能找到一个解,就全部退出,而是应该遍历所有、 #include <iostream> using namespace std; int n; int box[25]; bool visit[25]; int curside = 0; void DFS(int cursize, int pos) { //当前拿物品的值 if (cursize == 40) { curside++; return; } for (int i = pos; i < n; i++) { if (cursize + box[i] > 40 || visit[i] == true) { continue; } visit[i] = true;//暂时加入 DFS(cursize + box[i], i + 1); visit[i] = false; } return; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &box[i]); visit[i] == false; } DFS(0, 0); printf("%d\n", curside); return 0; }