题解 | #数据分类处理#

数据分类处理

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int numI = sc.nextInt();
        ArrayList<Integer> listI = new ArrayList<>();
	  	//记录I序列元素
        for (int i = 0; i < numI; i++) {
            listI.add(sc.nextInt());
        }
        int numR = sc.nextInt();
        HashSet<Integer> hashSet = new HashSet<>();
	  	//记录R序列元素的同时利用HashSet去重
        for (int i = 0; i < numR; i++) {
            hashSet.add(sc.nextInt());
        }
        ArrayList<Integer> listR = new ArrayList<>(hashSet);
	  	//R序列从小到大排序
        Collections.sort(listR);
        int sum = 0;
	  	//res记录每一个R序列有多少个符合要求的I序列元素
        int[] res = new int[listR.size()];
        ArrayList<Integer> arrayList = new ArrayList<>();
        int count = -1;
        for (Integer i : listR) {
            count++;
            String valueOfI = String.valueOf(i);
            for (int j = 0; j < listI.size(); j++) {
                if (String.valueOf(listI.get(j)).contains(valueOfI)){
				  	//符合要求的元素个数加一
                    res[count]++;
				  	//记录索引
                    arrayList.add(j);
				  	//记录元素的值
                    arrayList.add(listI.get(j));
                }
            }
        }
	  	//统计总共输出的数字个数
        for (int i : res) {
		  	//索引+元素本身,所以要乘2
            sum += i * 2;
            if (i != 0){
                sum += 2;
            }
        }

        System.out.print(sum + " ");
        for (int i = 0; i < res.length; i++) {
		  	//只有个数不为零时才输出
            if (res[i] == 0){
                continue;
            }
            System.out.print(listR.get(i) + " " + res[i] + " ");
            for (int j = 0; j < res[i]; j++) {
                System.out.print(arrayList.get(0) + " ");
                arrayList.remove(0);
                System.out.print(arrayList.get(0) + " ");
                arrayList.remove(0);
            }
        }
    }
}


全部评论

相关推荐

赛博小保安:你这简历没啥大问题的,经历技能也足够了,问题应该就是出在出身了,学院本就是这样,HR忙着跟92的勾搭呢,哪有心思看我们这些双非😿😭
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务