rambless
称砝码
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 n = in.nextInt();
//重量
int[] w_arr = new int[n];
for (int i = 0; i < n; i++) {
w_arr[i] = in.nextInt();
}
//数量
int[] n_arr = new int[n];
for (int i = 0; i < n; i++) {
n_arr[i] = in.nextInt();
}
//枚举所有情况、利用set去重:每种砝码(不加、加1个...加n_arr[i]个)
Set<Integer> list = new HashSet<>();
Set<Integer> li;
for (int i = 0; i < w_arr.length; i++) {
li = new HashSet<>();
for (int j = 0; j <= n_arr[i]; j++) {
int temp = w_arr[i] * j;
if (list.isEmpty()) {
list.add(temp);
} else {
for (Integer integer : list) {
li.add(temp + integer);
}
}
}
list.addAll(li);
}
System.out.println(list.size());
}
}
}
