题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
TreeMap<Long,Long> map = new TreeMap<Long,Long>();
int n = in.nextInt();
long x,y;
while(n>0){
x = in.nextLong();
y = in.nextLong();
if(map.containsKey(x)){
map.put(x,map.get(x)+y);
}else{
map.put(x,y);
}
n--;
}
for(Long key : map.keySet()){
System.out.println(key+" "+map.get(key));
}
}
}
查看11道真题和解析