题解 | #数据分类处理#

数据分类处理

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


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int[] I = new int[n];
            for (int i = 0; i < n; i++) {
                I[i] = sc.nextInt();
            }
            int m = sc.nextInt();
            int[] oldR = new int[m];
            for (int i = 0; i < m; i++) {
                oldR[i] = sc.nextInt();
            }
            List<Integer> ans = new ArrayList<>();
            TreeSet<Integer> setR = new TreeSet<>();
            for (int j : oldR) {
                setR.add(j);
            }
            int[] R = new int[setR.size()];
            int ii = 0;
            for (Integer x : setR) {
                R[ii] = x;
                ii++;
            }
            for (int i = 0; i < R.length; i++) {
                List<Integer> list = new ArrayList<>();
                list = getRes(I, R[i]);
                if (list.size() != 0) {
                    ans.add(R[i]);
                    ans.add(list.size() / 2);
                    ans.addAll(list);
                }
            }
            ans.add(0, ans.size());
            for (int i = 0; i < ans.size(); i++) {
                if(i == ans.size() - 1){
                    System.out.println(ans.get(i));
                }else{
                    System.out.print(ans.get(i) + " ");
                }
            }
        }
    }

    private static List<Integer> getRes(int[] I, int x) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < I.length; i++) {
            if (isContains(I[i], x)) {
                list.add(i);
                list.add(I[i]);
            }
        }
        return list;
    }

    private static boolean isContains(int num, int x) {
        String numStr = num + "";
        String xStr = x + "";
        return numStr.contains(xStr);
    }
}
全部评论

相关推荐

11-18 15:57
门头沟学院 Java
最终归宿是测开:这个重邮的大佬在重邮很有名的,他就喜欢打92的脸,越有人质疑他,他越觉得爽😂
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务