题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Map<Integer, Integer> data = new TreeMap(); int count = Integer.valueOf(in.nextLine()).intValue(); while (true) { String line = ""; try { line = in.nextLine(); } catch (Exception e) { } if (line.length() == 0) { break; } String[] lineA = line.split(" "); if (data.containsKey(Integer.valueOf(lineA[0]))) { Integer value = Integer.valueOf(lineA[1]) + data.get(Integer.valueOf(lineA[0])); data.put(Integer.valueOf(lineA[0]), value); continue; } data.put(Integer.valueOf(lineA[0]), Integer.valueOf(lineA[1])); } data.forEach((key, value) -> { System.out.println(key + " " + value); }); } }