题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Map<Integer,Integer> imputs = new HashMap<>(); int flag = 0; // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case if(flag == 0){ flag = in.nextInt(); continue; } flag -- ; int a = in.nextInt(); int b = in.nextInt(); Integer getB = imputs.get(a); if(getB == null){ imputs.put(a,b); }else{ int r = getB + b; imputs.put(a,r); } if (flag == 0){ Set<Integer> keys = imputs.keySet(); List<Integer> keyList = new ArrayList<>(keys); Collections.sort(keyList); for (Integer e: keyList) { System.out.println(e+" "+ imputs.get(e)); } } } } }
写的有点繁琐了,喜欢在idea上编辑好在粘贴进来。