题解 | #数据分类处理#

数据分类处理

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

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int Ino = in.nextInt();
            int[] is = new int[Ino];
            for(int i=0;i<Ino;i++){
                is[i]=in.nextInt();
            }
            int Rno = in.nextInt();
            HashSet<Integer> Rs = new HashSet<>();//存放数组R,hashset会自动去重
            for(int i=0;i<Rno;i++){
                Rs.add(in.nextInt());
            }
            Object[] Rsl=Rs.stream().sorted().toArray();//对去重后的数组R排序
            ArrayList<String> outlR = new ArrayList<>();//用于存放要输出的R(i)
            ArrayList<String> outlres = new ArrayList<>();//用于存放要输出的数组i的元素的下标和值
            ArrayList<Integer> outlRno = new ArrayList<>();//用于存放要输出的R(i)所对应的数量
            for(int r=0;r<Rsl.length;r++){
                String now=Rsl[r].toString();
                int no = 0;
                for(int l=0;l<is.length;l++){
                    String sl=String.valueOf(is[l]);
                    if(sl.contains(now)){
                        no+=1;
                        if(!outlR.contains(now)){outlR.add(now);}
                        outlres.add(String.valueOf(l));
                        outlres.add(String.valueOf(sl));
                    }
                }
                if(no>0){outlRno.add(no);}
            }
            //打印要输出的数字的个数(不含其本身)
            System.out.printf("%d ",outlR.size()+outlRno.size()+outlres.size()); 

            //打印其他部分
            int from=0;           
            for(int j=0;j<outlR.size();j++){
                System.out.printf("%s %d ",outlR.get(j),outlRno.get(j));               
                for(int n=from;n<from+outlRno.get(j)*2;n++){
                    System.out.printf("%s ",outlres.get(n));
                }
                from+=outlRno.get(j)*2;
            }
        }
    }
}

全部评论

相关推荐

05-16 11:16
已编辑
东华理工大学 Java
牛客73769814...:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
牛客创作赏金赛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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