rambless

数据分类处理

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 m = in.nextInt();
            List<String> iList = new ArrayList<>(m);
            for(int i=0; i<m; i++) {
                iList.add(Integer.toString(in.nextInt()));
            }
            int n = in.nextInt();
            List<Integer> rList = new ArrayList<>(n);
            Integer temp;
            for(int i=0; i<n; i++) {
                temp = in.nextInt();
                if(!rList.contains(temp)) {
                    rList.add(temp);
                }
            }
            Collections.sort(rList);
            List<Object> list = new ArrayList<>();
            int sum = 0;
            for(int i=0; i<rList.size(); i++) {
                int a = rList.get(i);
                int count = 0;
                String str;
                String t = "";
                for(int j=0; j<iList.size(); j++) {
                    str = iList.get(j);
                    if(str.contains(Integer.toString(a))) {
                        t += j + " " + str + " ";
                        count++;
                    }
                }
                if(count>0) {
                    list.add(a);
                    list.add(count);
                    list.add(t.trim());
                    sum += 2+2*count;
                }
            }
            System.out.print(sum+" ");
            for(int i=0; i<list.size(); i++) {
                if(i==list.size()-1) {
                    System.out.print(list.get(i));
                } else {
                    System.out.print(list.get(i) + " ");
                }
            }
        }
    }
}

全部评论

相关推荐

2025-12-11 14:24
门头沟学院 Java
牛客35720396...:不要用boss,全是骗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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