题解 | #称砝码#
称砝码
https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c
import java.io.IOException;
import java.util.Scanner;
import java.util.HashSet;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
int type = sc.nextInt();
int[] weight = new int[type];
int[] count = new int[type];
for(int i = 0; i < type; ++i){
weight[i] = sc.nextInt();
}
for(int i = 0; i < type; ++i){
count[i] = sc.nextInt();
}
HashSet<Integer> set = new HashSet<>();
set.add(0);
// 遍历砝码,每种砝码依次向电子称上加
for(int i = 0; i < weight.length; ++i){
ArrayList<Integer> list = new ArrayList(set);
// 遍历砝码的个数,每种砝码从1个开始往电子秤称上加
for(int j = 1; j <= count[i]; ++j){
// 对于已经称量过的砝码,目前所有可能的重量,加上j个未加的砝码之后所得的重量
for(int k = 0; k < list.size(); ++k){
// set去重
set.add(list.get(k) + weight[i] * j);
}
}
}
System.out.println(set.size());
}
}
