题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.io.BufferedInputStream; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(new BufferedInputStream(System.in)); Map<Integer, Integer> map = new TreeMap<>(); while (sc.hasNext()) { int rows = Integer.parseInt(sc.nextLine()); for (int i = 0; i < rows; i++) { String[] lines = sc.nextLine().split(" "); int key = Integer.parseInt(lines[0]); int value = Integer.parseInt(lines[1]); if (map.containsKey(key)) { map.put(key, map.get(key) + value); } else { map.put(key, value); } } map.forEach((k, v) -> System.out.println(k + " " + v)); } } }#java刷题#