题解 | #称砝码#
称砝码
https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
HashSet<Integer> set = new HashSet<>();
set.add(0);
int n = sc.nextInt();
int[] m = new int[n];
int[] x = new int[n];
for (int i = 0; i < n; i++) {
m[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
x[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
List<Integer> list = new ArrayList<>(set);
for (int j = 0; j <= x[i]; j++) {
for (int k = 0; k < list.size(); k++) {
set.add(list.get(k)+m[i]*j);
}
}
}
System.out.println(set.size());
}
}
}
查看10道真题和解析