-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_1.java
More file actions
39 lines (31 loc) · 931 Bytes
/
Copy pathStack_1.java
File metadata and controls
39 lines (31 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package HuaweiCoding;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
/*
* 数据表记录包含表索引和数值,
* 请对表索引相同的记录进行合并,
* 即将相同索引的数值进行求和运算,
* 输出按照key值升序进行输出。
*/
public class Stack_1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()) {
int num = sc.nextInt();
TreeMap<Integer, Integer> treeMap=new TreeMap<>();
for(int i=0;i<num;i++) {
int key = sc.nextInt();
int value = sc.nextInt();
if(treeMap.containsKey(key)) {
treeMap.put(key, treeMap.get(key)+value);
}else {
treeMap.put(key, value);
}
}
for (Entry<Integer,Integer> keyValues: treeMap.entrySet()) {
System.out.println(keyValues.getKey()+" "+keyValues.getValue());
}
}
}
}