题解 | #称砝码#

称砝码

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) { 
            int N = Integer.parseInt(in.nextLine());
            String[] weight = in.nextLine().split(" ");
            String[] ma = in.nextLine().split(" ");
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < N; i++) {
                map.put(Integer.parseInt(weight[i]), Integer.parseInt(ma[i]));
            }
            Map<Integer, Integer> res = new HashMap<>();
            res.put(0, 1);
            for (int b : map.keySet()) {
                for (int i = 0; i < map.get(b); i++) {
                    Set<Integer> set = res.keySet();
                    // 存入新的集合中
                    Set<Integer> newSet = new HashSet<>(set);//将key保存到新的set,否则put之后会越界
                    for (int c : newSet) {
                        res.put(c + b, 1);
                    }
                }
            }
            System.out.println(res.size());
        }
    }
}

全部评论

相关推荐

11-13 20:16
已编辑
厦门理工学院 软件测试
专业嗎喽:硕佬,把学校背景放后面几段,学校背景双非还学院,让人看了就不想往下看。 把实习经历和个人奖项放前面,用数字化简述自己实习的成果和掌握的技能,比如负责项目一次通过率90%,曾4次发现项目潜在问题风险为公司减少损失等等
点赞 评论 收藏
分享
牛客41406533...:回答他在课上学,一辈子待在学校的老教授用三十年前的祖传PPT一字一句的讲解,使用谭浩强红皮书作为教材在devc++里面敲出a+++++a的瞬间爆出114514个编译错误来学这样才显得专业
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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