题解 | #称砝码#

称砝码

https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c

import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int weightType = in.nextInt();
        int[] weightList = new int[weightType];
        int[] weightQuantity = new int[weightType];
        HashSet<Integer> resultSet = new
        HashSet<Integer>();// 存放所有可能得结果, 不担心重复
        resultSet.add(0);
        for (int i = 0; i < weightType; i++) {
            weightList[i] = (in.nextInt());
        }
        for (int i = 0; i < weightType; i++) {
            weightQuantity[i] = (in.nextInt());
        }
        for (int i = 0; i < weightType; i++) {
            ArrayList<Integer> list = new ArrayList<Integer>(resultSet);
            for (int j = 1; j <= weightQuantity[i]; j++) {
                for (int k = 0; k < list.size(); k++) {
                    resultSet.add(list.get(k) + weightList[i] * j);
                }

            }
        }
        System.out.println(resultSet.size());

    }
}

全部评论

相关推荐

10-25 13:59
门头沟学院 Java
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务