美文网首页
网易内推六道

网易内推六道

作者: _Kantin | 来源:发表于2017-08-15 17:07 被阅读62次

(1)例如: s = "ABAB",那么小易有六种排列的结果:
"AABB","ABAB","ABBA","BAAB","BABA","BBAA"
其中只有"AABB"和"BBAA"满足最多只有一对不同颜色的相邻砖块。
例如:输入ABAB,输出2
(用一个list统计所给字符串类型的个数,大于2的话则不成立,因为当且仅当两个两个字母的时候成立。否则的话至少能成立1个或者两个)

   public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        List<Character> list = new ArrayList<>();
        for(int i=0;i<str.length();i++){
            if(!list.contains(str.charAt(i))){
                list.add(str.charAt(i));
            }
        }
        if(list.size()>2){
            System.out.println(0);
        }else{
            System.out.println(list.size());
        }
    }

(2)判断一个数组是否是等差数组,是的话输出"Possible",否则输出"Impossible"。
(先用前两个数记录等差数组的间距,之后用一个count来判断是否完成了数组的遍历)

   public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int length = sc.nextInt();
        int[] arr = new int[length];
        for(int i=0;i<length;i++){
            arr[i]=sc.nextInt();
        }
        Arrays.sort(arr);
        int space = arr[1]-arr[0];
        int count=0;
        for(int i=1;i<arr.length-1;i++){
            if(arr[i+1]-arr[i]==space){
              count++;
            }
        }
        if(count==length-2){
            System.out.println("Possible");
        }else{
            System.out.println("Impossible");
        }
    }

(3)如果一个01串任意两个相邻位置的字符都是不一样的,我们就叫这个01串为交错01串。例如: "1","10101","0101010"都是交错01串。
输入:111101111 输出:3
(判断最长不等连续子串即可)

  public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int max=0,len=0;
        for(int i=1;i<str.length();i++){
            if(str.charAt(i-1)!=str.charAt(i)){
                len++;
                if(len>max){
                    max=len;
                }
            }else{
                len=1;
            }
        }
        System.out.println(max!=0?max:len);
    }

(4)小易有一个长度为n的整数序列,a_1,...,a_n。然后考虑在一个空序列b上进行n次以下操作:
1、将a_i放入b序列的末尾
2、逆置b序列
小易需要你计算输出操作n次之后的b序列。
例如输入:4 1 2 3 4 输出:4 2 1 3
(这是一道规律题,规律在哪里看注释)

  public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()){
            int n = sc.nextInt();
            int[] nums = new int[n];
            for (int i = 0; i < n; i++) {
                nums[i] = sc.nextInt();
            }
            for (int i = n - 1; i >= 0; i -= 2) {     // 前一半从最后一个数开始以2为步长递减
                System.out.print(nums[i] + " ");
            }
            for (int i = n % 2; i < n - 2; i += 2) {  // 后一半根据整数个数的奇偶,分别从第二个或第一个数开始以2为步长递增
                System.out.print(nums[i] + " ");
            }
            System.out.print(nums[n - 2]);  // 最后一个数
        }
    }

(5)小易为了向他的父母表现他已经长大独立了,他决定搬出去自己居住一段时间。一个人生活增加了许多花费: 小易每天必须吃一个水果并且需要每天支付x元的房屋租金。当前小易手中已经有f个水果和d元钱,小易也能去商店购买一些水果,商店每个水果售卖p元。小易为了表现他独立生活的能力,希望能独立生活的时间越长越好,小易希望你来帮他计算一下他最多能独立生活多少天。
(先看自己带的水果够吃几天,然后就先吃自己的水果并交房租,当水果吃完之后,则剩下的钱用来买水果和交房租)

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int rentPay=sc.nextInt();
        int appleNum=sc.nextInt();
        int totalMoney=sc.nextInt();
        int applePrice=sc.nextInt();

        //先看不买苹果够不够活,不能再考虑苹果的情况
        if(totalMoney/rentPay<=appleNum)
            System.out.println(totalMoney/rentPay);
        else
            //先边吃自己带的苹果并交房租,看看能坚持多久
            System.out.println(appleNum+(totalMoney-appleNum*rentPay)/(applePrice+rentPay));
        sc.close();
    }

(6)当队列排列顺序是: 25-10-40-5-25, 身高差绝对值的总和为15+30+35+20=100。
这是最大的疯狂值了。
输入:5 5 10 25 40 25 输出:100
(类似水桶原理,先定义最大的间隔,然后在一步步的缩小,然后给最后一个数找一个合适的位置即可(在中间))

 public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int n =sc.nextInt();
            int[] nums = new int[n];
            for(int i=0;i<n;i++){
                nums[i]=sc.nextInt();
            }
//先对数字进行排序之后一步步的缩小,注意缩小的规律
            Arrays.sort(nums);
            int min = nums[0];
            int max = nums[n-1];
            int diff =max-min;
            int minIndex = 1;
            int maxIndex=n-2;
            while(minIndex<maxIndex){
                diff+=max-nums[minIndex];
                diff+=nums[maxIndex]-min;
                min=nums[minIndex++];
                max=nums[maxIndex--];
            }
            diff+=Math.max(nums[maxIndex]-min,max-nums[minIndex]);
            System.out.println(diff);
        }
    }

相关文章

网友评论

      本文标题:网易内推六道

      本文链接:https://www.haomeiwen.com/subject/vhjvrxtx.html