题解 | #称砝码#
称砝码
https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c
// 值得mark一下 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.hasNextLine()) { // 注意 while 处理多个 case int n = Integer.valueOf(in.nextLine()); String[] ws = in.nextLine().split(" "); String[] ns = in.nextLine().split(" "); int[] weight = new int[n]; int[] num = new int[n]; for (int i = 0; i < n; i++) { weight[i] = Integer.valueOf(ws[i]); num[i] = Integer.valueOf(ns[i]); } Set<Integer> ansSet = new HashSet<>(); ansSet.add(0); for (int i = 0; i < n; i++) { List<Integer> oldAns = new ArrayList<>(ansSet); for (int j = 0; j <= num[i]; j++) { for (int x : oldAns) { ansSet.add(x + j * weight[i]); } } } System.out.println(ansSet.size()); } } }