题解 | #数据分类处理#

数据分类处理

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);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int ln=in.nextInt();//接收l数组长度
            int[] l=new int[ln];
            for(int i=0;i<ln;i++){
                l[i]=in.nextInt();//nextInt()接收每一个数据
            }
            int rn=in.nextInt();
            //TreeSet去重排序
            TreeSet<Integer> tmpr=new TreeSet<>();
            for(int i=0;i<rn;i++){
                tmpr.add(in.nextInt());
            }
            List<Integer> res=new ArrayList<>();
            for(int num:tmpr){
                //使用count计数每一个列表匹配数据的个数
                int count=0;
                //创建列表接收每一个R去重排序后在L中包含的数据
                List<Integer> res2=new ArrayList<>();
                for(int i=0;i<l.length;i++){
                    //转换为String类型,使用contains()方法判断包含关系
                    if(String.valueOf(l[i]).contains(String.valueOf(num))){
                        count++;
                        res2.add(i);
                        res2.add(l[i]);
                    }
                }
                if(count>0){
                    res.add(num);
                    //res2里存放了索引和数据
                    res.add(count);
                    res.addAll(res2);
                }
            }
            System.out.print(res.size());
            for(int i:res){
                System.out.print(" "+i);
            }
        }
    }
}

全部评论

相关推荐

半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
05-22 09:23
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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