暴力求解

数据分类处理

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

对于题目的解题思考过程记录如下:

(1)读取R序列,获取出来,进行去重和存储操作,使用TreeSet类集,遍历TreeSet便可以得到从小到大的R序列;

(2)读取I序列,利用数组,进行存储;

(3)根据R序列,遍历R序列,针对I序列进行扫描,获取符合条件的数据,再进行存储,存储过程使用list即可。

其中使用标志判断,是否具有符合R的I,具有,则进行标志位判断,将R序列存储进List中,然后,再去操作符合R的I序列

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            // 获取和存储序列I
            int icount = sc.nextInt();
            int[] iarray = new int[icount];
            for(int i=0; i<icount; i++){
                iarray[i] = sc.nextInt();
            }
            // 获取、去重、存储序列R
            int rcount = sc.nextInt();
            Set<Integer> set = new TreeSet<>();
            for(int i=0; i<rcount; i++){
                set.add(sc.nextInt());
            }
            // 遍历序列R
            // 存储有效数值
            List<Integer> list = new ArrayList<>();
            Iterator iter = set.iterator();
            while(iter.hasNext()){
                boolean isExist = false;
                // 规则数字
                int r = (int)iter.next();
                String rs = r + "";
                // 遍历数组,判断是否包含数字
                List<Integer> tempList = null;
                for(int i=0; i<iarray.length; i++){
                    // 判断是否包含
                    if((iarray[i] + "").contains(rs)){
                        if(isExist == false){
                            isExist = true;
                            list.add(r);
                        }
                        if(tempList == null){
                            tempList = new ArrayList<>();
                        }
                        tempList.add(i);
                        tempList.add(iarray[i]);
                    }
                }
                // 重新转移存储
                if(tempList != null){
                    list.add(tempList.size() / 2);
                    for(int x : tempList){
                        list.add(x);
                    }
                }
            }
            // 遍历最终结果集合
            if(list.size() > 0){
                System.out.print(list.size());
                System.out.print(" ");
                for(int i=0; i<list.size(); i++){
                    System.out.print(list.get(i));
                    System.out.print(i == list.size() - 1 ? "" : " ");
                }
            }
            System.out.println();
        }

    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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