题解 | 称砝码
称砝码
https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c
#include <iostream> #include <vector> #include <set> using namespace std; int main() { int n; cin >> n; vector<int> weight(n); vector<int> num(n); for(int i = 0; i < n; ++i) { cin >> weight[i]; } for(int i = 0; i < n; ++i) { cin >> num[i]; } set<int> weights; weights.insert(0); for(int i = 0; i < n; ++i) { vector<int> currentWeights(weights.begin(), weights.end()); for(int j = 1; j <= num[i]; ++j) { for(int k = 0; k < currentWeights.size(); ++k) { int newWeight = currentWeights[k] + j * weight[i]; weights.insert(newWeight); } } } cout << weights.size() << endl; return 0; }