题解 | #数据分类处理#

数据分类处理

https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd


import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int iSize = scan.nextInt();
        List<Integer> l_I = new ArrayList<>();
        Set<Integer> l_R = new TreeSet<>();
        for (int i = 0; i < iSize; i++) {
            l_I.add(scan.nextInt());
        }
        int r_size = scan.nextInt();
        for (int i = 0; i < r_size; i++) {
            l_R.add(scan.nextInt());
        }
        StringBuilder resData = new StringBuilder();
        for (Integer integer : l_R) {
            StringBuilder stringBuilder = new StringBuilder();
            int index = 0; //记录R元素出现的位置
            for (Integer i : l_I) {
                if (i.toString().contains(integer.toString())) {
                    stringBuilder.append(index).append(" ").append(i).append(" ");
                }
                index++;
            }
            if (!stringBuilder.toString().equals("")) {
                resData.append(integer).append(" ").append(stringBuilder.toString().split(" ").length/2).append(" ").append(stringBuilder); //数量为stringBuilder.toString().split(" ").length/2 
            }
        }
        String res = resData.toString().split(" ").length + " " + resData;
        System.out.println(res);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务