zoukankan      html  css  js  c++  java
  • 杀手-科大讯飞18.09.07校招笔试

    思路:就是模拟杀手杀人的过程,新开一个数组用于标记被杀死的位置。然后生成新的数组,直到平安夜来临。

    public class Main {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNext()) {
                int n = in.nextInt();
                int[] arr = new int[n];
                for (int i = 0; i < n; i++) {
                    arr[i] = in.nextInt();
                }
    
                boolean flag = true;
                int res = 0;
                while (flag) {
                    int died =0;
                    int[] kill = new int[arr.length];
                    //标记已经被杀死的位置
                    for (int i = 0; i < arr.length-1; i++) {
                        if(arr[i]>arr[i+1]){
                            kill[i + 1] = 1;
                            died++;
                        }
                    }
                    //死亡人数是0->今夜是平安夜
                    if(died==0){
                        System.out.println(res);
                        break;
                    }
    
                    //清除尸体
                    for (int i = 0; i < arr.length; i++) {
                        if (kill[i] == 1) {
                            arr[i] = 0;
                        }
                    }
    
                    //生成新数组
                    res++;
                    int newlen = arr.length - died;
                    int[] lived = new int[newlen];
                    int index = 0;
                    for (int i = 0; i < n; i++) {
                        if (arr[i] != 0) {
                            lived[index++] = arr[i];
                        }
                        if(index==newlen) break;
                    }
                    arr = lived;
                }
            }
        }
    }
    
  • 相关阅读:
    winston写日志(译)
    H5打字机特效
    Flutter 手指放大 平移 旋转 Widget
    51nod1432【贪心】
    死锁的例子
    C# SpinLock用法。
    鼓音效
    rm-rf
    cdoj 1334 郭大侠与Rabi-Ribi Label:贪心+数据结构
    1092 回文字符串(51nod)
  • 原文地址:https://www.cnblogs.com/keeya/p/9606847.html
Copyright © 2011-2022 走看看