题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); //获取I数据 int nI = in.nextInt(); int first = in.nextInt(); String[] arrI = in.nextLine().split(" "); arrI[0] = String.valueOf(first); // System.out.println(Arrays.toString(arrI)); //获取R数据 int nR = in.nextInt(); int[] arrR = new int[nR]; int i = 0; while (in.hasNextInt()) { arrR[i] = in.nextInt(); i++; } //排序 Arrays.sort(arrR); //去重 int slow = 0; for (int fast = 1; fast < nR; fast++) { while (fast < nR && arrR[fast] == arrR[slow]) { fast++; } if (fast < nR) { slow++; arrR[slow] = arrR[fast]; } } //去重后长度为slow + 1 //设置二维数组,记录满足条件的i的index int[][] result = new int[slow + 1][nI + 1]; for (int j = 0; j <= slow; j++) { int count = 0; for (int k = 0; k < nI; k++) { if (arrI[k].contains(String.valueOf(arrR[j]))) { result[j][k] = 1; count++; } } result[j][nI] = count; } //总共有多少个数 int sum = 0; for (int j = 0; j <= slow; j++) { if (result[j][nI] > 0) { sum += result[j][nI] * 2 + 2; } } //输出结果 System.out.print(sum + " "); for (int j = 0; j <= slow; j++) { if (result[j][nI] > 0) { System.out.print(arrR[j] + " "); System.out.print(result[j][nI] + " "); //循环输出index和对应值 for (int k = 0; k < nI; k++) { if (result[j][k] == 1) { System.out.print(k + " " + arrI[k] + " "); } } } } } }