题解 | #称砝码#

称砝码

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            HashSet<Integer> set = new HashSet<>();
            set.add(0);
            int [] arr = new int [a ];
            int [] num = new int [a] ;
            HashSet<Integer> resSet = new HashSet<>();
            for (int i = 0 ; i < a ; i ++) {
                arr[i] = in.nextInt();
            }
            for (int i = 0 ; i < a ; i ++) {
                num[i] = in.nextInt();
            }
            for (int i = 0 ; i < a ; i ++) {
                for (int j = 1; j <= num[i] ; j++) {
                    for (Integer number : set) {
                        resSet.add(number + arr[i] * j);
                    }

                }
                set.addAll(resSet);
            }
            System.out.println(set.size());
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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