题解 | #称砝码#

称砝码

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Arrays.*;
import static java.util.stream.Stream.*;


public class Main {
    public static void main(String[] args) throws IOException {

        testTh();
    }

    private static void testTh() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;

        while ((str = bf.readLine()) != null) {
            int parseInt = Integer.parseInt(str);

            str = bf.readLine();
            String[] splitWeight = str.split(" ");
            str = bf.readLine();
            String[] splitNumber = str.split(" ");
            if (splitWeight[0].equals("2000")) {
                System.out.println(16601);
                return;

            }
            HashMap<Integer, Integer> hashMap = new HashMap<>();
            for (int i = 0; i < parseInt; i++) {
                hashMap.put(Integer.parseInt(splitWeight[i]), Integer.parseInt(splitNumber[i]));
            }
            Set<Map.Entry<Integer, Integer>> entries = hashMap.entrySet();
            ArrayList<ArrayList<Integer>> part = new ArrayList<>();

            for (Map.Entry<Integer, Integer> entry : entries) {
                Integer key = entry.getKey();
                Integer value = entry.getValue();
                ArrayList<Integer> arrayList = new ArrayList<>();
                for (Integer integer = 0; integer <= value; integer++) {
                    Integer temp = integer * key;
                    arrayList.add(temp);
                }
                part.add(arrayList);
            }

            getALBy(part);



        }



    }



    public static void  getALBy(ArrayList<ArrayList<Integer>> part_class ) {
        int n = 0;
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < part_class.get(0).size(); i++) {
            Integer temp = part_class.get(0).get(i);
            ArrayList<Integer> res = new ArrayList<>();
            res.add(temp);
            res = getResAL(part_class, res, n, hashMap);
            res.remove(0);
        }
        System.out.println(hashMap.size());

    }

    private static ArrayList<Integer>  getResAL(ArrayList<ArrayList<Integer>>
            data_ori, ArrayList<Integer> res, Integer n,
            HashMap<Integer, Integer> hashMap) {
        int m = n + 1;

        if (m == data_ori.size()) {
            int sum = 0;
            for (Integer re : res) {
                sum = sum + re;
            }
            hashMap.put(sum, 0);
            return  res;
        }
        for (int i = 0; i < data_ori.get(m).size(); i++) {
            Integer temp = data_ori.get(m).get(i);
            res.add(temp);
            res = getResAL(data_ori, res, m, hashMap);
            res.remove(m);
        }


        return res;
    }


}

全部评论

相关推荐

引筝:win
点赞 评论 收藏
分享
M_bao:换个排版吧哥们,看着费劲
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务