题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
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(System.in); int n = sc.nextInt(); Map<Integer, Integer> map = new TreeMap<>(); for (int i = 0; i < n; i++) { int k = sc.nextInt(); int v = sc.nextInt(); map.put(k,map.getOrDefault(k,0) + v); } // 遍历输出 for (Integer integer : map.keySet()) { System.out.printf("%d %d\n",integer, map.get(integer)); } } }