华为机试:称砝码

#include <iostream>
#include <set>
#include <vector>
using namespace std;

vector<int> w;
set<int> ans;
// void dfs(int all, int index, int sum) {
//     if (index >= all) return;
//     for (int i = index; i < all; i++) {
//         ans.insert(w[i]);
//         sum += w[i];
//         ans.insert(sum);
//         dfs(all, i + 1, sum);
//     }
// }
int main() {
    int n;
    cin >> n;
    vector<int> m;
    int x;
    //int ans = 0;
    for (int i = 0; i < n; i++) {
        cin >> x;
        m.push_back(x);
        //ans += x;
    }
    for (int j = 0; j < n; j++) {
        cin >> x;
        for (int i = 0; i < x; i++) {
            w.push_back(m[j]);
        }
    }
    // dfs(w.size(), 0, 0);
    ans.insert(0);
    for (auto h : w) {
        set<int> s(ans);                      //防止无限自增
        for (auto it = s.begin(); it != s.end(); it++) {
            ans.insert(*it + h);
        }
    }
    cout << ans.size() << endl;
    return 0;
}

全部评论
代码正常运行吗
点赞 回复 分享
发布于 2023-03-28 21:54 四川

相关推荐

每晚夜里独自颤抖:把华北改为华南再试一试,应该就没啥问题了。改完可能都不用投,别人主动联系了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务