1 solutions

  • 0
    @ 2022-3-8 21:24:17
    #include <bits/stdc++.h>
    using namespace std;
    int n,m,opt;
    int s[25];
    bool ss[25];
    vector<int>v;
    void dfs(int sum,int layer){
    	if(layer==0&&sum==m){
    		bool flag=0;
    		for(int i=1;i<=n;i++){
    			if(ss[i]){
    				flag=1;
    				break;
    			}
    		}
    		if(!flag) return ;
    		opt++;
    		for(int i=1;i<=n;i++){
    			if(ss[i]) cout<<s[i]<<" ";
    		}
    		printf("\n");
    	}
    	if(!layer) return ;
    	for(int i=0;i<=1;i++){
    		ss[layer]=i;
    		i==1?dfs(sum+s[layer],layer-1):dfs(sum,layer-1);
    	}
    }
    int main(){
    	cin>>n;
    	for(int i=1;i<=n;i++){
    		cin>>s[i];
    	}
    	cin>>m;
    	dfs(0,n);
    	cout<<opt;
    	return 0;
    }
    • 1

    Information

    ID
    1186
    Time
    1000ms
    Memory
    512MiB
    Difficulty
    8
    Tags
    # Submissions
    16
    Accepted
    5
    Uploaded By