题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.util.*; public class Main { public static void main(String[] args) { Scanner fzhinput = new Scanner(System.in); int num = fzhinput.nextInt(); int result[][]= new int[num][2]; int i,j; for(i=0;i<num;i++){ result[i][0]= fzhinput.nextInt(); result[i][1] = fzhinput.nextInt(); } for(i=0;i<num;i++){ for(j=num-1;j>i;j--){ if(result[i][0]==result[j][0]){ result[i][1]= result[i][1]+result[j][1]; result[j][0]=-1; } } } int[][] filteredResult = Arrays.stream(result) .filter(arr -> arr[0] != -1) .toArray(int[][]::new); Arrays.sort(filteredResult, Comparator.comparingInt(arr -> arr[0])); for(i=0;i<filteredResult.length;i++){ System.out.println(filteredResult[i][0]+" "+filteredResult[i][1]); } } }