题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()){ int N = Integer.parseInt(in.nextLine()); Map<Integer,Integer> res = new HashMap<>(); int[] sum = new int[N]; for (int i=0;i<N;i++){ String str = in.nextLine(); String[] strings = str.split(" "); sum[i] = Integer.parseInt(strings[0]); if (res.get(sum[i])==null) res.put(sum[i],Integer.parseInt(strings[1])); else res.put(sum[i],Integer.parseInt(strings[1])+res.get(sum[i])); } for (int i=0;i<N-1;i++){ for(int j = i+1;j<N;j++){ if (sum[i]>sum[j]) { int ll = sum[i]; sum[i] = sum[j]; sum[j] = ll; } } } for (int i=0;i<N;i++){ if (i==0||sum[i]!=sum[i-1]) System.out.println(sum[i]+" "+res.get(sum [i])); } } } }