题解 | #称砝码#

称砝码

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

最简单最容易理解的解法

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String str ="";
        while((str =  reader.readLine()) !=null){

            String str1 = reader.readLine();
            String str2 = reader.readLine();
            int len = Integer.parseInt(str);
            int[] a = new int[len];
            int[] b = new int[len];
            String[] c = str1.split(" ");
            String[] d = str2.split(" ");
            Set<Integer> set = new HashSet();
            for (int i = 0; i < c.length; i++) {//依次遍历每个砝码
                Integer[] x = new Integer[set.size()];
                set.toArray(x);
                int s1 = Integer.parseInt(c[i]);
                int s2 = Integer.parseInt(d[i]);
                while(s2>0) {//依次遍历砝码数
                    set.add(s1 * s2);//每个砝码与砝码数相乘
                    for (Integer n : x) {//将前一个砝码可能性与当前砝码称重相加
                        set.add(n+s1*s2);
                    }
                    s2--;
                }

            }
            System.out.println(set.size()+1);
        }
    }


}
全部评论

相关推荐

合不合适,我自己说了才算
码农索隆:hr:“真执着啊,来我公司当法人吧”
点赞 评论 收藏
分享
06-12 10:50
门头沟学院 Java
你的不定积分没加C:我怎么在学院群看到了同样的话
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
3
1
分享

创作者周榜

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