题解 | #称砝码#

称砝码

https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c

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

int main(int argc, char* argv[]){
    int n;
    cin >> n;
    vector<int> weights(n, 0);
    for(int i = 0; i < n; ++i){
        cin >> weights[i];
    }
    vector<int> amounts(n , 0);
    for(int i = 0; i < n; ++i){
        cin >> amounts[i];
    }
    unordered_set<int> kinds;
    kinds.insert(0);
    for(int i = 0; i < weights.size(); ++i){
        for(int j = 1; j <= amounts[i]; ++j){
            unordered_set<int> temp(kinds);
            for(auto iter = temp.begin(); iter != temp.end(); ++iter){
                kinds.insert(*iter + weights[i]);
            }
        }
    }
    cout << kinds.size() << endl;
    return 0;
}
全部评论

相关推荐

06-02 15:53
阳光学院 Java
点赞 评论 收藏
分享
鬼迹人途:你去投一投尚游游戏,服务器一面,第一个图算法,做完了给你一个策略题,你给出方案他就提出低概率问题,答不上当场给你挂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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