#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 105;
int weights[MAX_N];
int selectedWeights[MAX_N];
int itemCount, targetWeight;
bool solutionFound = false;
void dfs(int depth, int selectedCount, int currentWeight) {
if (currentWeight == targetWeight) {
for (int i = 1; i <= selectedCount; i++) {
cout << selectedWeights[i] << " ";
solutionFound = true;
}
cout << endl;
return;
} else {
if (currentWeight > targetWeight || depth > itemCount || solutionFound == true) return;
selectedWeights[selectedCount + 1] = weights[depth];
dfs(depth + 1, selectedCount + 1, currentWeight + weights[depth]);
dfs(depth + 1, selectedCount, currentWeight);
}
}
int main() {
cin >> itemCount >> targetWeight;
for (int i = 1; i <= itemCount; i++) {
cin >> weights[i];
}
dfs(1, 0, 0);
if (!solutionFound) {
cout << "No Answer!" << endl;
}
}
链接描述