题解 | #称砝码#

称砝码

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

import java.io.IOException;
import java.util.Scanner;
import java.util.HashSet;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) throws IOException{
        Scanner sc = new Scanner(System.in);
        int type = sc.nextInt();
        int[] weight = new int[type];
        int[] count = new int[type];
        for(int i = 0; i < type; ++i){
            weight[i] = sc.nextInt();
        }
        for(int i = 0; i < type; ++i){
            count[i] = sc.nextInt();
        }
        HashSet<Integer> set = new HashSet<>();
        set.add(0);
        // 遍历砝码,每种砝码依次向电子称上加
        for(int i = 0; i < weight.length; ++i){
            ArrayList<Integer> list = new ArrayList(set);
            // 遍历砝码的个数,每种砝码从1个开始往电子秤称上加
            for(int j = 1; j <= count[i]; ++j){
                // 对于已经称量过的砝码,目前所有可能的重量,加上j个未加的砝码之后所得的重量
                for(int k = 0; k < list.size(); ++k){
                    // set去重
                    set.add(list.get(k) + weight[i] * j);
                }
            }
        }
        System.out.println(set.size());
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 11:24
大家还是用ai改吧,我心疼得要死,就当花钱买教训吧,人家直接拿完钱就跑路了
程序员小白条:简历修改700....神奇,又不是帮你面试,咋的,简历修改从双非变92了还是没实习变成有大厂实习了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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