import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] idx = new int[n];//保存key int[] value = new int[n];//保存value for (int i = 0; i < n; i++) { idx[i] = in.nextInt(); value[i] = in.nextInt(); } Map<Integer,Integer> map = new TreeMap<>();//注意利用TreeMap的自动排序特性(以int作为key时) for (int i = 0; i < n; i++) { if (map.get(idx[i]) == null) {//首次出现,直接插入 map.put(idx[i], value[i]); } else {//已经存在,则合并value值后插入 int tmp = (int) map.get(idx[i]); map.put(idx[i], tmp + value[i]); } } for (Integer key : map.keySet()) {//遍历输出 System.out.print(key + " "); System.out.println(map.get(key)); } } }