题解 | #称砝码#

称砝码

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        // 砝码的重量
        int[] weights = new int[n];
        // 砝码的个数
        int[] counts = new int[n];
        for (int i = 0; i < n; i++) {
            weights[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
            counts[i] = sc.nextInt();
        }
        TreeSet<Integer> set = new TreeSet<>();
        // 初始化重量0
        set.add(0);
        // 遍历砝码
        for (int i = 0; i < n; i++) {
            // 存所有结果
            ArrayList<Integer> list = new ArrayList<>(set);
            // 遍历个数
            for (int j = 1; j <= counts[i]; j++) {
                for (int k = 0; k < list.size(); k++) {
                    set.add(list.get(k) + weights[i] * j);
                }
            }
        }
        System.out.println(set.size());
    }
}

全部评论

相关推荐

坦荡的牛油果拿到了s...:简历最好压缩在一页,期望职位和其他没必要单独一个板块,竞赛经历合并到教育经历里,而且最好获奖了才写。最重要的是实习经历,写得有点简单 一眼看不出来参与的项目具体是什么,具体需求、问题是怎么挖掘、分析、解决的。但是才大一就这么卷了吗
点赞 评论 收藏
分享
迷茫的大四🐶:都让开,我tm来啦
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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