题解 | #合并表记录#

合并表记录

http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

import java.util.; import java.io.; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = null; while((str = in.readLine()) != null){ Integer num = Integer.parseInt(str); TreeMap<Integer,Integer> map = new TreeMap<>(); while(num -- > 0){ str = in.readLine(); String[] arr = str.split(" "); int m = Integer.parseInt(arr[0]); int n = Integer.parseInt(arr[1]); if(!map.containsKey(m)){ map.put(m,n); } else{ map.put(m,map.get(m) + n); } } for(Integer k : map.keySet()){ System.out.println(k +" " + map.get(k));

        }
    }
}

} // import java.util.; // public class Main { // public static void main(String[] args){ // Scanner sc = new Scanner(System.in); // TreeMap<Integer,Integer> map = new TreeMap<>(); // while(sc.hasNext()){ // int n = sc.nextInt(); // for(int i =0;i<n;i++){ // int key = sc.nextInt(); // int value = sc.nextInt(); // map.put(key,map.getOrDefault(key,0)+value); // } // for(Integer i : map.keySet()){ // System.out.println(i+" "+map.get(i)); // } // } // } // } // import java.util.; // import java.io.*; // public class Main{ // public static void main(String[] args)throws IOException{ // BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // String str = null; // TreeMap<Integer,Integer> map = new TreeMap<>(); // while((str = in.readLine()) != null){ // int num = Integer.parseInt(str); // while(num -- > 0){ // String[] s = in.readLine().trim().split(" "); // int key = Integer.parseInt(s[0]); // int value = Integer.parseInt(s[1]); // if(map.containsKey(key)){ // value += map.get(key); // map.put(key,value); // }else{ // map.put(key,value); // }

// } // } // for(Integer i : map.keySet()){ // System.out.println(i +" "+map.get(i)); // } // } // }

我居南半坡 文章被收录于专栏

多刷题,积蓄力量,欢迎讨论

全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
04-02 21:36
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务