京东笔试编程题详解
来源:互联网 发布:linux tomcat重启 编辑:程序博客网 时间:2024/05/16 06:37
题目
大意是小B喜欢买东西到了商店之后发现店员还没有把价签贴到货架上,但是又等不及要买 ,因此想先把东西放在购物篮,拿到店员处根据价签算价。现在有价格表和购物表,你能帮ta预估一下最好的情况和最坏的情况下需要多少钱吗?
输入第一行是 n m ,n代表店里有多少个商品,m 代表购物栏有多少商品
输入第二行是 n个用空格隔开的整数代表不同商品的价格 (可重复)
接下来是m行商品名 用字符串表示要求输出最小总价和最大总价
输出示例
6 3
1 2 5 3 6 8
banana
banana
apple
则最小价格应该是4,最大价格是22
输出示例
4 22具体代码如下:
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); }}
0 0
- 京东笔试编程题详解
- 网易笔试编程题详解
- 京东编程笔试题
- 2016京东笔试编程题
- 京东笔试编程后记
- 奇虎360 2017春招笔试编程题详解
- 2016京东在线笔试(编程题之一)
- 编码-京东实习笔试编程题-生日礼物-动态规划
- 京东笔试编程题:进制均值(C++)
- 2017京东在线笔试编程题之 买东西
- 2016京东算法工程师实习生笔试编程题:上台阶
- 笔试编程题一道
- Shell编程笔试题
- 笔试编程题
- 网络编程笔试题
- 笔试编程题
- 笔试编程题
- 常见笔试编程题
- 微信支付统一下单
- 在V7包中,不能进行actionbar的主题的修改,可以在使用
- TCP/IP之三:ICMP
- 欢迎使用CSDN-markdown编辑器
- CentOS安装 JDK1.7
- 京东笔试编程题详解
- POI_EXCEL
- PHP $_SERVER
- PAT|1067. Sort with Swap(0,*)
- Hi3516A开发--根文件系统
- Git服务器的搭建
- Java和JavaScript的关系
- Linux基础(一)
- SHELL脚本-OMC超时补报