-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo8.java
36 lines (31 loc) · 879 Bytes
/
Demo8.java
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
package HUAWEI;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.SortedMap;
import java.util.TreeMap;
public class Demo8 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
SortedMap<Integer,Integer> map= new TreeMap<Integer,Integer>();
int n= Integer.parseInt(sc.nextLine());
//°ÑÊý¾Ý´æÈëmap
for(int i=0;i<n;i++){
String[] mid= sc.nextLine().split("\\s");
int key=Integer.parseInt(mid[0]);
int value=Integer.parseInt(mid[1]);
if(map.containsKey(key)){
map.put(key, map.get(key)+value);
}else{
map.put(key, value);
}
}
//Êä³ö
Iterator<Map.Entry<Integer,Integer>> it=map.entrySet().iterator();
while(it.hasNext()){
Entry<Integer,Integer> entry=it.next();
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}