题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.util.Scanner;
import java.util.Map;
import java.util.TreeMap;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
Map<Integer, Integer> tMap = new TreeMap<>();
for (int i = 0; i < n; i++) {
if (sc.hasNextLine()) {
String str = sc.nextLine();
String[] strArr = str.split(" ");
Integer i1 = Integer.parseInt(strArr[0]);
Integer i2 = Integer.parseInt(strArr[1]);
if (tMap.containsKey(i1)) {
tMap.put(i1, i2 + tMap.get(i1));
} else {
tMap.put(i1, i2);
}
//System.out.println(tMap.toString());
}
}
tMap.forEach((key, value) -> {
System.out.println(key + " " + value);
});
}
}
荣耀工作强度 439人发布