题解 | #称砝码#

称砝码

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

全部评论

相关推荐

02-23 12:32
已编辑
门头沟学院 嵌入式工程师
King987:学历没有问题,然后既然有实习经历的话,把这个放在上面多写一点,哪怕你自己包装一下,只要能圆回来就行,既然有实习经历的话,肯定主要看实习经历之类的。然后也会主要问这里多准备准备
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务