我是靠谱客的博主 外向睫毛膏,这篇文章主要介绍京东笔试编程题详解,现在分享给大家,希望可以做个参考。

题目
大意是小B喜欢买东西到了商店之后发现店员还没有把价签贴到货架上,但是又等不及要买 ,因此想先把东西放在购物篮,拿到店员处根据价签算价。现在有价格表和购物表,你能帮ta预估一下最好的情况和最坏的情况下需要多少钱吗? 

输入第一行是 n m ,n代表店里有多少个商品,m 代表购物栏有多少商品 
输入第二行是 n个用空格隔开的整数代表不同商品的价格 (可重复) 
接下来是m行商品名 用字符串表示

要求输出最小总价和最大总价

输出示例 
6 3 
1 2 5 3 6 8 
banana 
banana 
apple 
则最小价格应该是4,最大价格是22 
输出示例 
4 22

具体代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package offer; import java.util.*; /** * Created by computerDell on 2016/9/5. */ public class Shopping { public static void main(String[] args) { doMain(); } public static void doMain(){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); int m=sc.nextInt(); int [] price =new int[n]; String [] buyName=new String[m]; for (int i = 0; i <price.length ; i++) { price[i]=sc.nextInt(); } sc.nextLine(); for (int i = 0; i <buyName.length ; i++) { buyName[i]=sc.nextLine(); } // insertSort(price); Map<String,Integer> map=saveBuy(buyName); int min=0,max=0; min=computeMin(price,map,m); max=computeMax(price,map,m); System.out.println(min+" "+max); } } private static int computeMax(int[] price, Map<String, Integer> map, int m) { int max=0; int i=price.length-1; Iterator<Map.Entry<String,Integer>> it=map.entrySet().iterator(); while(it.hasNext()){ Map.Entry<String,Integer> entry=it.next(); max+=entry.getValue()*price[i]; i--; } return max; } private static int computeMin(int[] price, Map<String, Integer> map, int m) { int min=0; int i=0; Iterator<Map.Entry<String,Integer>> it=map.entrySet().iterator(); while(it.hasNext()){ Map.Entry<String,Integer> entry=it.next(); min+=entry.getValue()*price[i]; i++; } return min; } private static Map<String,Integer> saveBuy(String[] buyName) { HashMap<String,Integer> hashMap=new HashMap<String,Integer>(); int count=0; for (int i = 0; i < buyName.length; i++) { String name=buyName[i]; if(hashMap.containsKey(name)){ count=1+hashMap.get(name); hashMap.put(name,count); }else{ hashMap.put(name,1); } } TreeComparator treeComparator=new TreeComparator(hashMap); Map<String,Integer> treeMap=new TreeMap(treeComparator); treeMap.putAll(hashMap); return treeMap; } private static void insertSort(int[] price) { int i,j; for(i=1;i<price.length;i++){ int tmp=price[i]; for(j=i;j>0&&tmp<price[j-1];j--){ price[i]=price[i-1]; } price[j]=tmp; } } } class TreeComparator implements Comparator<String>{//map从大到小排列 HashMap<String,Integer> base; public TreeComparator(HashMap<String,Integer> base){ this.base=base; } @Override public int compare(String o1, String o2) { return base.get(o2)-base.get(o1); } }

最后

以上就是外向睫毛膏最近收集整理的关于京东笔试编程题详解的全部内容,更多相关京东笔试编程题详解内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部