题解 | #称砝码#

称砝码

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

import java.util.*;
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            int zhonglei = Integer.valueOf(br.readLine());
            String[] height = br.readLine().split(" ");
            String[] count = br.readLine().split(" ");
            HashSet hset = new HashSet();
            hset.add(0);
            for (int i = 0; i < zhonglei; i++) {
                List<Integer> list = new ArrayList(hset);
                for (int j = 1; j <= Integer.valueOf(count[i]); j++) {
                    for (Integer m : list) {
                        hset.add(m + j * Integer.valueOf(height[i]));
                    }
                }
            }
            System.out.println(hset.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务