题解 | #合并表记录#
合并表记录
http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
利用TreeMap自排序 进行处理数据
然后直接输出即可
import java.util.*; public class Main { public static void main(String[] args){ TreeMap<Integer, Integer> map = new TreeMap<>(); Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for(int i = 0; i < n; i++){ int index = scanner.nextInt(); int value = scanner.nextInt(); map.put(index, map.getOrDefault(index, 0) + value); } for (Map.Entry<Integer, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } } }