题解 | #称砝码#

称砝码

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<Integer> list = new ArrayList<>();
        Set<Integer> set = new HashSet<>();
        set.add(0);
        while (--n >= 0) {
            list.add(in.nextInt());
        }
        for (int i = 0; i < list.size(); i++) {
            int x = in.nextInt();
            List<Integer> temp = new ArrayList<>();
            for (Integer it : set) {
                for (int j = 0; j <= x; j++) {
                    temp.add(it + list.get(i) * j);
                }
            }
            set.addAll(temp);
        }
        System.out.println(set.size());

    }
}

全部评论

相关推荐

10-28 10:48
已编辑
门头沟学院 Java
孩子我想要offer:发笔试后还没笔试把我挂了,然后邮箱一直让我测评没测,后面不知道干嘛又给我捞起来下轮笔试,做完测评笔试又挂了😅
点赞 评论 收藏
分享
挥毫自在:想白嫖你呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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