题解 | #称砝码#

称砝码

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

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        HashSet<Integer> set = new HashSet<>();// 采用集合,不用担心重复问题
        set.add(0);// 初始化为0
        int n = in.nextInt(); // 个数
        int[] w = new int[n];
        int[] nums = new int[n];
        // 砝码重量
        for(int i=0;i<n;i++){
            w[i] = in.nextInt();
        }
        // 对应的个数
        for(int i=0;i<n;i++){
            nums[i] = in.nextInt();
        }

        for(int i=0;i<n;i++){ // 遍历砝码
            ArrayList<Integer> list = new ArrayList<>(set);// 用来保存各种情况
            for(int j=1;j<=nums[i];j++){ // 遍历个数
                for(int k=0;k<list.size();k++){// 对每一种情况都加砝码
                    set.add(list.get(k)+w[i]*j);
                }
            }
        }
        System.out.println(set.size());
    }
}

全部评论

相关推荐

02-17 20:43
西北大学 Java
在做测评的猫头鹰很紧张:他问你,你问deep seek
点赞 评论 收藏
分享
Aaso:挺好的,早挂早超生
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务