题解 | #称砝码#

称砝码

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());
    }
}

全部评论

相关推荐

码农索隆:单休一个月少休息4天,一年就是48天,平时节假日,别人3天假期,单休的两天
点赞 评论 收藏
分享
05-12 11:09
已编辑
门头沟学院 后端
已注销:没必要放这么多专业技能的描述。这些应该是默认已会的,写这么多行感觉在凑内容。项目这块感觉再包装包装吧,换个名字,虽然大家的项目基本都是网上套壳的,但是你这也太明显了。放一个业务项目,再放一个技术项目。技术项目,例如中间件的一些扩展和尝试。
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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