题解 | #合并表记录#
合并表记录
http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
//TreeMap自动排序去重,key已存在就做加法更新value
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<Integer,Integer> treeMap = new TreeMap<>();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0;i < n;i++){
int a = sc.nextInt();
int b = sc.nextInt();
if (treeMap.containsKey(a)){
treeMap.put(a,treeMap.get(a)+b);
}else {
treeMap.put(a,b);
}
}
for (int i:treeMap.keySet()
) {
System.out.println(i+" "+treeMap.get(i));
}
}
}