4.11webank笔试情况

选择题40分,三个编程题60分。
第一题,将数字转为16进制然后看里面字母的数量。
public class Solution1 {
    @Test
    public void myTest() {

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        String str = Integer.toHexString(x);
        int res = 0;
        for (int i = 0; i < str.length(); i++) {
            if (Character.isLetter(str.charAt(i))) res++;
        }
        System.out.println(res);
    }

}

第二题,给一个数组,看左边x和右边y以内是否是最小值,是的话输出第一个就行了。比如下面的输出位置4(从1开始).
        //10 2 3
    //10 8 7 1 9 2 6 4 3 5
    //6 2 2
    //
    //10 8 2 7 3 1
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), x = sc.nextInt(), y = sc.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
            int flag1 = 1, flag2 = 1;
            int cur = nums[i];
            int left = i - 1, right = i + 1, left_n = x, right_n = y;
            while (left >= 0 && left_n != 0) {
                if (nums[left] <= cur) {
                    flag1 = 0;
                    break;
                }
                left--;
                left_n--;
            }
            while (right < n && right_n != 0) {
                if (nums[right] <= cur) {
                    flag2 = 0;
                    break;
                }
                right++;
                right_n--;
            }
            if (flag1 == 0 || flag2 == 0) continue;
            else {
                System.out.println(i + 1);
                break;
            }
        }
    }        
第三题,给n为长度的字符串,判断其子串(i到j的字符串)能被k整除的个数,比如3为字符串,121,字串由1,12,121,2,21,1共六个,k是3的话,12,21可以被3整除,所以返回2.
前面所有加起来也没有半小时,后面一个半小时全在做这道题,最开始只能过9%,想了半天发现字符长度1000可能会超时,关键牛客这次超时这种运行时错误没提醒,我就一直以为代码问题,改了改最后只能过27%。
public class Solution3 {
    @Test
    public void myTest() {
        long x = Long.parseLong("122222222222");
        System.out.println(x);
    }

    /*小明有一个长度为n的仅由0到9组成的字符串。小美想知道这个串里有多少个子串代表的十进制数能被k整除。
      字符串a的子串即是那些可被描述为“由a中第i至第j个字符组成的字符串”的串。
      如字符串‘121’有‘1’,‘2’,‘1’,‘12’,‘21’,‘121’六个子串。
        输出一个整数,代表给出的数字串中有多少个子串满足其对应的十进制数能被k整除。
    如样例中的‘1101’,‘012’,‘12’,‘0’都能被3整除。
    如输入:5 3
            11012
      输出  4
    n(1<=n<=1000),k(1<=k<=1000)
    ‘1101’,‘012’,‘12’,‘0’
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt(), k = sc.nextInt();
            String str = sc.next();
            long res = 0;
            for (int i = 0; i < str.length(); i++) {
                long x = 0;
                for (int j = i; j < str.length(); j++) {
                    x = x * 10 + (str.charAt(j) - '0');
                    if (x % k == 0) res++;
                }
            }
            System.out.println(res);
        }
    }
}







#微众银行#
全部评论
三题BigInteger一把梭
点赞 回复 分享
发布于 2022-04-15 14:25

相关推荐

星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
评论
2
9
分享

创作者周榜

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