题解 | #合并表记录#
合并表记录
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); Map<Integer,Integer> map = new HashMap<>(); int num = Integer.valueOf(in.nextLine()); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String a = in.nextLine(); String[] b = a.split(" "); int keyNum = Integer.valueOf(b[0]); int value = Integer.valueOf(b[1]); if(map.get(keyNum) == null){ map.put(keyNum,value); }else{ map.put(keyNum,map.get(keyNum)+value); } } List<Integer> lista = new ArrayList<>(); for(int k : map.keySet()){ lista.add(k); } Collections.sort(lista); for(int k : lista){ int value = map.get(k); System.out.println(k+" "+value); } } }#练习时长两年半#