京东面试题 股票委托

import java.util.*; public class Main { public static void main(String[] args) {
        Scanner cin = new Scanner(System.in); while (cin.hasNext()) {
            Map<Integer,Integer> S = new TreeMap<>(new Comparator<Integer>(){ @Override  public int compare(Integer o1, Integer o2) { return o2-o1;
                }
            });
            Map<Integer,Integer> B = new TreeMap<>(new Comparator<Integer>(){ @Override  public int compare(Integer o1, Integer o2) { return o2-o1;
                }
            }); int n = cin.nextInt(); int s = cin.nextInt(); for (int i = 0; i <n; i++) {
                String flagB = cin.next(); if (flagB.equals("B")){ int one = cin.nextInt(); int two = cin.nextInt(); if (B.containsKey(one)){
                        B.put(one,B.get(one)+two);
                    }else {
                        B.put(one,two);
                    }
                }else { int one = cin.nextInt(); int two = cin.nextInt(); if (S.containsKey(one)){
                        S.put(one,S.get(one)+two);
                    }else {
                        S.put(one,two);
                    }
                }
            } // 显示  int countS = 0;
            Iterator itS = S.keySet().iterator(); while (itS.hasNext()&&countS<s){
                Object key = itS.next();
                Object value = S.get(key);
                System.out.println("S "+key+" "+value);
                countS++;
            } int countB = 0;
            Iterator itB = B.keySet().iterator(); while (itB.hasNext()&&countB<s){
                Object key = itB.next();
                Object value = B.get(key);
                System.out.println("B "+key+" "+value);
                countB++;
            }
        }
    }
}
全部评论

相关推荐

xwqlikepsl:感觉很厉害啊,慢慢找
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务