提交时间:2024-04-11 14:39:25

运行 ID: 143011

#include<iostream> using namespace std; int w[1001],ans[1001],id,s,n; void dfs(int num,int weight){//已经遍历num个物品,当前背包重量weight if(num == n){ if(weight == s){ for(int i = 1;i <= id;i++){ cout << ans[i] << endl; } exit(0); } return ; } if(weight>s) return ; dfs(num+1,weight); ans[++id] = w[num+1]; dfs(num+1,weight+w[num+1]); id--; } int main(){ cin >> s >> n; for(int i = 1;i <= n;i++){ cin >> w[i]; } dfs(0,0); cout << "Failed!"; return 0; }