网易 2021年 9 月 19 日开发岗 笔试真题

网易 9.19 笔试
1、涂格子:
将手中的一排格子涂上红蓝两种颜色,每个格子牛牛都有自己的想法,例如 1号和2号格子涂蓝色,3号格子涂红色,4号格子涂蓝色,5号格子涂红色,67涂蓝色,8号涂红色。 按照这个方法,他需要6步。但是他也可以将1-7号涂上蓝色,再将3号5号8号涂成红色,则步数最少,为4次。 但是目前数据量为500000,请你给出最少的操作次数。

输入:
8
BBRBRBBR (B表示 蓝色,R表示 红色)
输出:
4

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        String str = in.next();
        int blueCount = 0;
        int tag = 0;
        for (int i = 0; i < num; i++) {
            if (str.charAt(i) == 'B' && tag == 0) {
                tag = 1;
            }
            if (tag == 1 && str.charAt(i) == 'R') {
                tag = 0;
                blueCount ++;
            }
        }
        int redCount = 0;
        tag = 0;
        for (int i = 0; i < num; i++) {
            if (str.charAt(i) == 'R' && tag == 0) {
                tag = 1;
            }
            if (tag == 1 && str.charAt(i) == 'B') {
                tag = 0;
                redCount ++;
            }
        }
        if (str.charAt(num-1) == 'R') {
            redCount++;
        }
        else {
            blueCount++;
        }
        System.out.println(Math.min(redCount,blueCount) + 1);
    }
}

2、数字变换
给出两个数字a、b,a每次可以乘上一个大于1的正整数得到新的a,我们将这个操作称为它乘。 现在请你计算,a是否可以通过若干次它乘变换为b。 若能则输出它乘次数,否则输出 -1。

输入:
2 16
输出:
3 (因为2 -> 4 -> 8 -> 16)


输入:
3 5
输出:
-1

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num1 = in.nextInt();
        int num2 = in.nextInt();
        if (num1 > num2 ) {
            System.out.println(-1);
        }
        else {
            Solution s = new Solution();
            int ans = s.factorize(num1, num2);
            System.out.println(ans);
        }
    }
}

class Solution{
    public int factorize(int num1, int num2) { //  因式 分解
        if (num2% num1 != 0) {
            return  -1;
        }
        int n = num2 / num1;
        StringBuilder res = new StringBuilder();
        int i = 2; // 定义 最小 素数
        // 进行辗转相除法
        while (i <= n) {
            // 若num 能整除 i ,则i 是num 的一个因数
            if (n % i == 0) {
                res.append(i);
                n = n / i;
                i = 2; // 能整除,则将i重新置为2
            } else {
                // 若无法整除,则i 自增
                i++;
            }
        }
        return res.length();
    }
}

3、更改 英文字母(子字符串)
对于一个只包含英文字母的字符串,你可以更改其中的字符串,不同的更改类型代价如下:
{
将一个字母改为另一个大小写相同的字母花费为5
将一个字母由大写改为小写或者由小写改为大写的花费为5
} 现在请你求出对于任给的一个只包含英文字母的字符串,使之包含有子串AcMer的最小代价是多少。

输入:
Acmer / AcMer / jkhAcmer
输出:
5 / 0 / 5 (m更改成 M / 不用更改 / m更改成 M)

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        int res = 50;
        int len = s.length();
        char[] temp = new char[]{'A','c','M','e','r'};
        for (int i = 0; i < len - 4; i++) {
            String substr = s.substring(i,i+5);
            int sum = 0;
            for (int j = 0; j < 5; j++) {
                int cost = 10;
                if (Math.abs(substr.charAt(j) - temp[j]) == 32) { // 同一字母的大小写,sum加5
                    cost -= 5;
                }
                else if (substr.charAt(j) == temp[j]) { // 相等,sum加0
                    cost -= 10;
                }
                else if (substr.charAt(j)>'a' && substr.charAt(j)<'z' && temp[j]>'a' && temp[j]<'z'||
                        substr.charAt(j)>'A' && substr.charAt(j)<'Z' && temp[j]>'A' && temp[j]<'Z') { // 同处于 一个 大写 或 小写 的层次
                    cost -= 5;
                }
                // 否则,转化为 另一个字母,并更改大小写,加10
                sum += cost;  // 对应的大小写没有关系
            }
            res = Math.min(res,sum);
        }
        System.out.println(res);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值