/* A Taobao order contains n (10>=n>=1) kinds of commodities A1, A2, ..., An, and the quantity of each commodity is a1, a2, ..., an, denoted as {a1, a2,...,an}(ak>0). During the production process of the order in the warehouse, in order to improve the operation efficiency, the warehouse will pre-package the popular combination products in advance. Suppose these n commodities have m (9>=m>=1) commodity combinations, and each combination bomk contains A1, A2, ..., the quantity of An is {b1, b2, ..., bn}( bk>=0, there is at least one bk>0) An example is as follows: The order contains A, B, C products, the quantity is {2, 3, 1}, the product combination bom1{2,1,1}, bom2{1,1,0}, bom3{0,1,1} Matching the given product combination to the above order, the possible matching results obtained are: res1. One set of combination 1 is matched, and the remaining product B; res2. Two sets of combination 2 are matched, and one set of combination 3 is matched, and there is no product left; The optimal matching of orders is now required. The principle of optimal matching is: 1. After matching the combination, the less the number of remaining commodity types, the better; 2. In the case of the same number of remaining commodity types, the less the number of matching combinations. the better; For example, in the above example, we think res2 is better than res1. Now you need to write a program, the input format is: n,m a1,a2,...,an bom1,b11,b12,...,b1n bom2,b21,b22,...,b2n .... boom,bm1,bm2,...,bmn Format description of input data (separate data with English commas): The first line of data: n products, m prepackaged solutions The second line of data: the number of items 1, the number of items 2,. . . , the number of items n The third row of data: bom1, the number of goods 1, the number of goods 2,. . . , the number of items n Data on line n-1: . . . . The nth row of data: bomn, the number of items 1, the number of items 2,. . . , the number of items n Find the optimal match for the input data, and output the optimal matching combination and number of sets, such as output for the above example: match result: bom2*2,bom3*1 Note: When there are multiple output results, it can be out of order Number of words entered: 1562 / 10000 Instructions for running programming - Compiler version: Java 1.8.0_66 Please use standard input and output (System.in, System.out); graphics, file, network, system-related operations are disabled, such as java.lang.Process , javax.swing.JFrame , Runtime.getRuntime; do not customize the package name , otherwise an error will be reported, that is, do not add statements such as package answer; you can write many classes, but there must be a class named Main with a public attribute, and Main is the only public class, and the Main class must contain A static method (function) named 'main', this method is the entry point of the program Time limit: 3S (languages other than C/C++: 5S) Memory limit: 128M (languages other than C/C++: 640M) enter: Format description of the input data (the data are separated by English commas): The first line of data: n products, m pre-packaged plans The second line of data: 1 product, 2 products,. . . , the number of n items in the third row of data: bom1, the number of items 1, the number of items 2,. . . , the data of the n-1th row of n items: . . . . The nth row of data: bomn, the number of items 1, the number of items 2,. . . , the number of items n output: Output the best matching combination and number of sets Input example: 3,3 2,3,1 bom1,2,1,1 bom2,1,1,0 bom3,0,1,1 Example output: match result: bom2*2 bom3*1 */ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class Main { /** Please complete the following function to achieve the function required by the title**/ /** Of course, you can also answer not according to this template, but according to your own ideas ^-^ **/ public static void main(String[] args) { List<Integer> order = new ArrayList<Integer>(); Map<String, List<Integer>> boms = new HashMap<String, List<Integer>>(); Scanner in = new Scanner(System.in); String line = in.nextLine(); Integer n = Integer.parseInt(line.split(",")[0]); Integer m = Integer.parseInt(line.split(",")[1]); line = in.nextLine(); String[] itemCnt = line.split(","); for(int i = 0; i < n ; i++){ order.add(Integer.parseInt(itemCnt[i])); } for(int i = 0; i < m; i++){ line = in.nextLine(); String[] bomInput = line.split(","); List<Integer> bomDetail = new ArrayList<Integer>(); for(int j = 1; j <= n; j++ ){ bomDetail.add(Integer.parseInt(bomInput[j])); } boms.put(bomInput[0], bomDetail); } in.close(); Map<String, Integer> res = resolve(order, boms); System.out.println("match result:"); for(String key : res.keySet()){ System.out.println(key+"*"+res.get(key)); } } // write your code here public static Map<String, Integer> resolve(List<Integer> order, Map<String, List<Integer>> boms) { Map<String, Integer> map=new HashMap<String, Integer>(); return map; } }
Alibaba 2018 Spring Intern Recruitment Written Exam Questions
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325950673&siteId=291194637
Recommended
Ranking