import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
TreeMap<Integer, Integer> map = new TreeMap<>();
int n=Integer.parseInt(in.nextLine());
String line="";
for(int i=0;i<n;i++){
line= in.nextLine();
int index=Integer.valueOf(line.split(" ")[0]);
int value=Integer.valueOf(line.split(" ")[1]);
int old=map.getOrDefault(index,0);
map.put(index,value+old);
}
for(int key:map.keySet()){
System.out.println(key+" "+map.get(key));
}
}
}
- 使用TreeMap时,必须进行import,这里import java.util.*
- 不要nextInt和nextLine混用,容易搞混。