题目描述
数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。
输入描述:
先输入键值对的个数
然后输入成对的index和value值,以空格隔开
输出描述:
输出合并后的键值对(多行)
输入例子:
4 0 1 0 2 1 2 3 4
输出例子:
0 3 1 2 3 4
思路1:刚开始我想用List来存储,但是List的索引需要从0开始,若输入键值对的键(索引)不从0开始,则会出错;然后考虑使用HashMap来存储,每次输入的键值进行判断,若是键相同,则值相加,并且覆盖对应原来key的value;最后简单输入几个数字后,感觉可以,但是OJ评判错误,发现HashMap存储的数据并不一定全是有序的,少数样本有序,多数就会产生乱序,若想得到正确结果,还需进行HashMap的排序(代码在后);然后想到使用TreeMap,TreeMap内部默认按照key升序排列,符合要求;
代码如下:
1 import java.util.Map; 2 import java.util.Scanner; 3 import java.util.TreeMap; 4 5 public class Temp { 6 7 public static void main(String[] args) { 8 Scanner sc = new Scanner(System.in); 9 Map<Integer, Integer> m =new TreeMap<Integer, Integer>(); 10 while(sc.hasNextInt()){ 11 int n=sc.nextInt(); 12 for(int i=0;i<n;i++){ 13 int key = sc.nextInt(); 14 int value = sc.nextInt(); 15 if(m.containsKey(key)){ 16 m.put(key, m.get(key)+value); 17 } 18 else 19 m.put(key, value); 20 } 21 for(Object key1 : m.keySet()){ 22 System.out.print(key1); 23 System.out.print(" "); 24 System.out.println(m.get(key1)); 25 } 26 } 27 } 28 29 }
思路2:其实和思路1差不多,只是使用HashMap来存储,然后需要重新对HashMap按照key进行升序排序;这里参考别人思路,有两种方法排序
代码1如下:
1 import java.util.Arrays; 2 import java.util.HashMap; 3 import java.util.Map; 4 import java.util.Scanner; 5 6 7 public class Temp { 8 9 public static void main(String[] args) { 10 Scanner sc = new Scanner(System.in); 11 Map<Integer, Integer> m =new HashMap<Integer, Integer>(); 12 while(sc.hasNextInt()){ 13 int n=sc.nextInt(); 14 for(int i=0;i<n;i++){ 15 int key = sc.nextInt(); 16 int value = sc.nextInt(); 17 if(m.containsKey(key)){ 18 m.put(key, m.get(key)+value); 19 } 20 else 21 m.put(key, value); 22 } 23 24 Object[] key_arr = m.keySet().toArray();//返回一个包含HashMap中所有key的数组 25 Arrays.sort(key_arr); //对key进行升序排列 26 for(Object key1 : key_arr){ 27 System.out.print(key1); 28 System.out.print(" "); 29 System.out.println(m.get(key1)); 30 } 31 } 32 } 33 34 }
代码2如下:
1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Comparator; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.Map.Entry; 8 import java.util.Scanner; 9 10 11 public class Temp2 { 12 13 public static void main(String[] args) { 14 Scanner sc = new Scanner(System.in); 15 Map<Integer, Integer> m =new HashMap<Integer, Integer>(); 16 while(sc.hasNextInt()){ 17 int n=sc.nextInt(); 18 for(int i=0;i<n;i++){ 19 int key = sc.nextInt(); 20 int value = sc.nextInt(); 21 if(m.containsKey(key)){ 22 m.put(key, m.get(key)+value); 23 } 24 else 25 m.put(key, value); 26 } 27 28 List<Map.Entry<Integer, Integer>> li = new ArrayList<Map.Entry<Integer,Integer>>(m.entrySet()); 29 Collections.sort(li, new Comparator<Map.Entry<Integer,Integer>>() { 30 31 @Override 32 public int compare(Entry<Integer, Integer> o1, 33 Entry<Integer, Integer> o2) { 34 // TODO Auto-generated method stub 35 return o1.getKey().compareTo(o2.getKey()); 36 } 37 }); 38 39 for(Map.Entry<Integer, Integer> map:li){ 40 System.out.println(map.getKey()+" "+map.getValue()); 41 } 42 43 } 44 } 45 46 }
参考资料:对TreeMap与HashMap重新排序