题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int[][] array = null; int linecnt = 1; int cnt = Integer.MAX_VALUE; int index; int v; String line; String[] lineArr; int fromIndex = 0; int[] temp; while (in.hasNextLine()) { // 注意 while 处理多个 case if (linecnt == 1) { cnt = in.nextInt(); array = new int[cnt][]; } else { line = in.nextLine(); if ("".equals(line)) { continue; } lineArr = line.split(" "); index = Integer.parseInt(lineArr[0]); if (index > array.length - 1) { fromIndex = array.length - 1; } else { fromIndex = index; } v = Integer.parseInt(lineArr[1]); for (int i = fromIndex; i >= 0; i--) { temp = array[i]; if (temp == null) { temp = new int[2]; temp[0] = index; temp[1] = v; array[i] = temp; break; } if (temp[0] == index) { temp[1] = temp[1] + v; break; } else { temp[0] = temp[0] ^ index; index = temp[0] ^ index; temp[0] = temp[0] ^ index; temp[1] = temp[1] ^ v; v = temp[1] ^ v; temp[1] = temp[1] ^ v; } } } if (linecnt > cnt) { break; } linecnt ++; } for (int i = 0; i < array.length; i++) { if (array[i] != null) { System.out.println(array[i][0] + " " + array[i][1]); } } } }