题解 | #合并表记录#

合并表记录

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

import java.util.*; public class Main { public static void main(String [] args){ Scanner sc = new Scanner(System.in); Map<Integer, Integer> map = new HashMap<>(); int num = sc.nextInt(); Set set = new TreeSet<>(); sc.nextLine(); for (int i = num; i > 0; i--) { String str= sc.nextLine(); String[] s= str.split(" "); int key=Integer.valueOf(s[0]); int value=Integer.valueOf(s[1]); set.add(key); if (map.get(key) != null) { map.put(key, map.get(key) +value); } else { map.put(key,value); } } for(int i :set){ System.out.print(i+" "); System.out.println(map.get(i)); }

  }

}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务