题解 | #神奇的口袋#

神奇的口袋

https://www.nowcoder.com/practice/9aaea0b82623466a8b29a9f1a00b5d35

#include<cstdio>
#include<vector>
using namespace std;
vector<int> vec;
int n;
bool visit[25];
int count = 0;
void DFS(int cursize,int pos){
    if(cursize == 40){
        ++count;
        return;
    }
    for(int i = pos ; i< n ;++i){
        if(cursize  + vec[i] > 40 || visit[i] == true ){
            continue;
        }else{
            visit[i] = true;
            DFS(cursize+vec[i],i+1);
            visit[i] = false;
        }
    }
}
int main() {
    int  x;
    scanf("%d", &n);
    for (int i = 0 ; i < n; ++i) {
        scanf("%d", &x);
        vec.push_back(x);
    }
     for (int i = 0 ; i < 25; ++i) {
       visit[i] = false;
    }
    DFS(0,0);
    printf("%d\n",count);
}

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务