题解 | #称砝码#
称砝码
https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c
set:加入当前数量的砝码之前能产生的重量种类
import java.util.HashSet; import java.util.Scanner; // 注意类名必须为 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[] m = new int[n]; for (int i = 0; i < n; i++) { m[i] = in.nextInt(); } int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = in.nextInt(); } HashSet<Integer> set = new HashSet<>(); set.add(0); for (int i = 0; i < n; i++) { Integer[] array = set.toArray(new Integer[0]); for (int j = 0; j <= x[i]; j++) { for (Integer integer : array) { set.add(integer + m[i] * j); } } } System.out.println(set.size()); } } }