题解 | #合并表记录#
合并表记录
http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
//据题意可知此处需要用到TreeMap,利用自动排序的特性
TreeMap<Integer,Integer> treeMap = new TreeMap<>();
int length = scanner.nextInt();
for(int i =0; i<length; i++){
int k = scanner.nextInt();
int v = scanner.nextInt();
if(treeMap.containsKey(k))
treeMap.put(k,treeMap.get(k)+v);
else
treeMap.put(k,v);
}
for(Map.Entry entry : treeMap.entrySet()){
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
}