题解 | #坐标移动#
合并表记录
http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//HashMap存储键值对
Map<Integer,Integer> list = new HashMap<Integer,Integer>();
for(int i=0; i<n; i++){
int key = sc.nextInt(), val = sc.nextInt();
list.put(key, list.getOrDefault(key,0)+val);
}
// 列表存储键并排序
ArrayList<integer> keys = new ArrayList<integer>();
for(Integer k:list.keySet()){
int i =0;
keys.add(k);
i++;
}
keys.sort(Comparator.naturalOrder());
// 遍历存储键的列表,从HashMap中取值
for(Integer key : keys){
System.out.println(key+" " + list.get(key));
}
}
}</integer></integer>