京东笔试编程题详解

来源:互联网 发布: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
原创粉丝点击