用map记录并做累加,对key排序后输出
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),key=0,ni=0;
HashMap<Integer,Integer> mp=new HashMap<Integer,Integer>();
//用map接收输入并累加
while(n-->0){
key=sc.nextInt();
mp.put(key,mp.getOrDefault(key,0)+sc.nextInt());
}
//创建排序数组
int[] ks=new int[mp.size()];
//将key值放入数组并排序
for(Integer i:mp.keySet()){
ks[ni++]=i;
}
Arrays.sort(ks);
//从小到大输出
for(Integer i:ks){
System.out.println(i+" "+mp.get(i));
}
}
}