蓝桥杯第十五届模拟赛第二期-Java组

 叠甲:

   博主只是一个0基础的大一新生,能力有限,所以可能有很多题目的思路比较莽。

 

1.

问题描述

  小蓝要在屏幕上放置一行文字,每个字的宽度相同。
  小蓝发现,如果每个字的宽为 36 像素,一行正好放下 30 个字,字符之间和前后都没有任何空隙。
  请问,如果每个字宽为 10 像素,字符之间不包含空隙,一行可以放下多少个字?

答案提交

  这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

纯送分的,结果是108

2.

问题描述

  求 2**2023%1000,即 2的2023次方除以1000的余数。

答案提交

  这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

 

 数字太大了要用BigInteger,最后的结果是 608

import java.math.BigInteger;
public class Main {
    public static void main(String[] args) {
        BigInteger b1=new BigInteger("2").pow(2023);
        BigInteger b2=new BigInteger("1000");
        System.out.println(b1.remainder(b2));
    }
}

 3.

问题描述

  如果一个正整数转化成二进制与转换成八进制后所有数位的数字之和相等,则称为数位和相等的数。
  前几个数位和相等的正整数为 1, 8, 9, 64, ……
  请问第 23 个数位和相等的正整数是多少?

答案提交

  这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

 用tostring来实现各种进制的转换

public class Main {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 1; i < 9999; i++) {
            long num1 = Long.parseLong(Integer.toString(i,2));
            long num2 = Long.parseLong(Integer.toString(i,8));
            if (sum(num1) == sum(num2)) {
                count++;
            }
            if (count == 23) {
                System.out.println("结果是:"+i);
                break;
            }
        }
    }
    public static long sum(long num) {
        long sum = 0;
        while (num > 0) {
            sum += num % 10;
            num /= 10;
        }
        return sum;
    }
}

最后结果是 4169

4.

问题描述

  对于以下这些数(6行,每行6个,共36个),请问约数个数最多的是哪个?(如果有多个,请回答出现最早的那个)
  393353 901440 123481 850930 423154 240461
  373746 232926 396677 486579 744860 468782
  941389 777714 992588 343292 385198 876426
  483857 241899 544851 647930 772403 109929
  882745 372491 877710 340000 659788 658675
  296521 491295 609764 718967 842000 670302

答案提交

  这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

 我还是直接莽的,结果是 901440

public class Main {
    public static void main(String[] args) {
        int[] numbers = {
                393353, 901440, 123481, 850930, 423154, 240461,
                373746, 232926, 396677, 486579, 744860, 468782,
                941389, 777714, 992588, 343292, 385198, 876426,
                483857, 241899, 544851, 647930, 772403, 109929,
                882745, 372491, 877710, 340000, 659788, 658675,
                296521, 491295, 609764, 718967, 842000, 670302
        };

        int maxDivisors = 0;
        int result = 0;

        for (int num : numbers) {
            int divisors = countDivisors(num);
            if (divisors > maxDivisors) {
                maxDivisors = divisors;
                result = num;
            }
        }

        System.out.println(result);
    }

    public static int countDivisors(int n) {
        // 计算一个数的约数个数
        int count = 0;
        for (int i = 1; i <= n; i++) {
            if (n % i == 0) {
                count++;
            }
        }
        return count;
    }
}

 5.

问题描述

  小蓝有一个01矩阵。他打算将第一行第一列的 0 变为 2 。变化过程有传染性,每次 2 的上下左右四个相邻的位置中的 0 都会变成 2 。直到最后每个 2 的周围都是 1 或 2 结束。
  请问,最终矩阵中有多少个 2 ?
  以下是小蓝的矩阵,共 30 行 40 列。
  0000100010000001101010101001001100000011
  0101111001111101110111100000101010011111
  1000010000011101010110000000001011010100
  0110101010110000000101100100000101001001
  0000011010100000111111001101100010101001
  0110000110000000110100000000010010100011
  0100110010000110000000100010000101110000
  0010011010100110001111001101100110100010
  1111000111101000001110010001001011101101
  0011110100011000000001101001101110100001
  0000000101011000010011111001010011011100
  0000100000011001000100101000111011101100
  0010110000001000001010100011000010100011
  0110110000100011011010011010001101011011
  0000100100000001010000101100000000000010
  0011001000001000000010011001100101000110
  1110101000011000000100011001001100111010
  0000100100111000001101001000001010010001
  0100010010000110100001100000110111110101
  1000001001100010011001111101011001110001
  0000000010100101000000111100110010101101
  0010110101001100000100000010000010110011
  0000011101001001000111011000100111010100
  0010001100100000011000101011000000010101
  1001111010010110011010101110000000101110
  0110011101000010100001000101001001100010
  1101000000010010011001000100110010000101
  1001100010100010000100000101111111111100
  1001011010101100001000000011000110110000
  0011000100011000010111101000101110110001

答案提交

  这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

我用的是dfs,算出来是 541,打表出来2边上好像没看到有0。但我们学长用bfs做出来是591。

这里贴一个我的代码:

仅供参考,如果有大佬发现错误请告诉我。

import java.util.Arrays;

public class Main {
    static int[] moveX = {0, 1, 0, -1};
    static int[] moveY = {1, 0, -1, 0};
    static int count = 1;

    public static void main(String[] args) {
        String str =
                "2000100010000001101010101001001100000011" +
                "0101111001111101110111100000101010011111" +
                "1000010000011101010110000000001011010100" +
                "0110101010110000000101100100000101001001" +
                "0000011010100000111111001101100010101001" +
                "0110000110000000110100000000010010100011" +
                "0100110010000110000000100010000101110000" +
                "0010011010100110001111001101100110100010" +
                "1111000111101000001110010001001011101101" +
                "0011110100011000000001101001101110100001" +
                "0000000101011000010011111001010011011100" +
                "0000100000011001000100101000111011101100" +
                "0010110000001000001010100011000010100011" +
                "0110110000100011011010011010001101011011" +
                "0000100100000001010000101100000000000010" +
                "0011001000001000000010011001100101000110" +
                "1110101000011000000100011001001100111010" +
                "0000100100111000001101001000001010010001" +
                "0100010010000110100001100000110111110101" +
                "1000001001100010011001111101011001110001" +
                "0000000010100101000000111100110010101101" +
                "0010110101001100000100000010000010110011" +
                "0000011101001001000111011000100111010100" +
                "0010001100100000011000101011000000010101" +
                "1001111010010110011010101110000000101110" +
                "0110011101000010100001000101001001100010" +
                "1101000000010010011001000100110010000101" +
                "1001100010100010000100000101111111111100" +
                "1001011010101100001000000011000110110000" +
                "0011000100011000010111101000101110110001";
        int[][] nums = new int[30][40];
        int index = 0;
        for (int i = 0; i < 30; i++) {
            for (int j = 0; j < 40; j++) {
                nums[i][j] = str.charAt(index++) ^ 48;
            }
        }
        boolean[][] visited = new boolean[30][40];
        dfs(0, 0, nums, visited);
        for (int i = 0; i < 30; i++) {
            System.out.println(Arrays.toString(nums[i]));
        }
        System.out.println(count);

    }

    public static void dfs(int x, int y, int[][] nums, boolean[][] visited) {
        if (nums[x][y] == 2) {
            for (int i = 0; i < 4; i++) {
                int nextX = x + moveX[i];
                int nextY = y + moveY[i];
                if (isValid(nextX, nextY, nums) && nums[nextX][nextY] == 0) {
                    visited[nextX][nextY] = true;
                    nums[nextX][nextY] = 2;
                    count++;
                    dfs(nextX, nextY, nums, visited);
                    visited[nextX][nextY] = false;
                }
            }
        }
    }

    static boolean isValid(int x, int y, int[][] map) {
        return x >= 0 && x < map.length && y >= 0 && y < map[0].length;
    }
}

这边再贴一个我写的bfs,算出来还是541 

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    static class point {
        int x;
        int y;
        int step;

        public point(int x, int y, int step) {
            this.x = x;
            this.y = y;
            this.step = step;
        }
    }

    static int[] moveX = {0, 1, 0, -1};
    static int[] moveY = {1, 0, -1, 0};
    static int count = 1;
    static Queue<point> queue = new LinkedList<>();

    public static void main(String[] args) {
        String str =
                "2000100010000001101010101001001100000011" +
                        "0101111001111101110111100000101010011111" +
                        "1000010000011101010110000000001011010100" +
                        "0110101010110000000101100100000101001001" +
                        "0000011010100000111111001101100010101001" +
                        "0110000110000000110100000000010010100011" +
                        "0100110010000110000000100010000101110000" +
                        "0010011010100110001111001101100110100010" +
                        "1111000111101000001110010001001011101101" +
                        "0011110100011000000001101001101110100001" +
                        "0000000101011000010011111001010011011100" +
                        "0000100000011001000100101000111011101100" +
                        "0010110000001000001010100011000010100011" +
                        "0110110000100011011010011010001101011011" +
                        "0000100100000001010000101100000000000010" +
                        "0011001000001000000010011001100101000110" +
                        "1110101000011000000100011001001100111010" +
                        "0000100100111000001101001000001010010001" +
                        "0100010010000110100001100000110111110101" +
                        "1000001001100010011001111101011001110001" +
                        "0000000010100101000000111100110010101101" +
                        "0010110101001100000100000010000010110011" +
                        "0000011101001001000111011000100111010100" +
                        "0010001100100000011000101011000000010101" +
                        "1001111010010110011010101110000000101110" +
                        "0110011101000010100001000101001001100010" +
                        "1101000000010010011001000100110010000101" +
                        "1001100010100010000100000101111111111100" +
                        "1001011010101100001000000011000110110000" +
                        "0011000100011000010111101000101110110001";
        int[][] map = new int[30][40];
        boolean[][] visited = new boolean[30][40];
        int index = 0;
        for (int i = 0; i < 30; i++) {
            for (int j = 0; j < 40; j++) {
                map[i][j] = str.charAt(index++) ^ 48;
            }
        }
        queue.add(new point(0, 0, 0));
        while (!queue.isEmpty()) {
            point po = queue.poll();
            int x = po.x;
            int y = po.y;
            int step = po.step;
            if (map[x][y] == 2) {
                for (int i = 0; i < 4; i++) {
                    int nextX = x + moveX[i];
                    int nextY = y + moveY[i];
                    if (isValid(nextX, nextY, map)
                            && map[nextX][nextY] == 0
                            && !visited[nextX][nextY]) {
                        point next = new point(nextX, nextY, step + 1);
                        map[nextX][nextY]=2;
                        queue.add(next);
                        count++;
                        visited[nextX][nextY] = true;//标记
                    }
                }
            }
        }
        for (int i = 0; i < 30; i++) {
            System.out.println(Arrays.toString(map[i]));
        }
        System.out.println(count);
    }

    static boolean isValid(int x, int y, int[][] map) {
        return x >= 0 && x < map.length && y >= 0 && y < map[0].length;
    }
}

 

打表出来长这个样子

c274b2a4cea845e5a26b4fc14a6ded25.png

6.

问题描述

  给定一个正好六位的正整数 x,请将 x 循环左移一位后输出。
  所谓循环左移一位,是指将原来的十万位变为个位,原来的万位到个位向左移动依次变为十万位到十位。
  例如:194910 左移一位变为 949101 。
  又如:987123 左移一位变为 871239 。

输入格式

  输入一行包含一个整数 x 。保证输入的 x 正好包含 6 个十进制数位,而且十万位和万位上的数字均不为 0 。

输出格式

  输出一行包含一个整数,表示答案。

样例输入

194910

样例输出

949101

 这题我的思路是用substring截取下标1之后的所有字符串然后和首个字符拼起来。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        // 将字符串左移一位
        String result = str.substring(1) + str.charAt(0);

        System.out.println(Integer.parseInt(result));
    }
}

7.

问题描述

  给定一个序列 a[1], a[2], …, a[n] ,如果对于某个下标 i ,a[i+1]<a[i],则称序列在 i 到 i+1 这个位置下落,落差为 a[i]-a[i+1] 。
  请问序列的最大落差是多少?

输入格式

  输入的第一行包含一个整数 n ,表示序列的长度。
  第二行包含 n 个整数,相邻的整数之间使用一个空格分隔,表示给定的序列。

输出格式

  输出一行包含一个整数,表示答案。如果没有位置下落,输出 0 。

样例输入

5
4 5 3 9 6

样例输出

3

我使用动态规划做的

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] sequence = new int[n];
        for (int i = 0; i < n; i++) {
            sequence[i] = scanner.nextInt();
        }

        int max = 0;
        for (int i = 0; i < n - 1; i++) {
            int current = sequence[i] - sequence[i + 1];
            max=Math.max(current,max);//状态转移方程
        }

        System.out.println(max);
    }
}

8.

问题描述

  输入一个仅包含小写英文字母的字符串,请问这个字符串中的最后一元音是什么。
  在英文中,a, e, i, o, u 共 5 个字母是元音字母,其它字母不是元音字母。

输入格式

  输入一行包含一个字符串,仅由小写英文字符组成,字符串中至少包含一个元音字母。

输出格式

  输出一行包含一个字符,表示答案。

样例输入

lanqiao

样例输出

o

样例输入

cup

样例输出

u

 从后往前遍历,找到元音字母直接return

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char[] cs=scanner.next().toCharArray();
        for(int i= cs.length-1;i>=0;i--){
            switch (cs[i]){
                case 'a':
                    System.out.println('a');
                    return;
                case 'e':
                    System.out.println('e');
                    return;
                case 'i':
                    System.out.println('i');
                    return;
                case 'o':
                    System.out.println('o');
                    return;
                case 'u':
                    System.out.println('u');
                    return;
            }
        }
    }
}

9.

问题描述

  给定一个整数,对这个整数的一次转换是指将这个整数变为这个整数的所有数位上的非零数字的乘积。
  例如,对 123456789 进行一次转换变为 1*2*3*4*5*6*7*8*9=362880,再进行一次转换变为 3*6*2*8*8=2304,再进行一次转换变为 2*3*4=24,再进行一次转换变为 8。
  给定一个整数,请依次将转换过程中经历的每个整数输出,直到小于 10 。

输入格式

  输入一行包含一个整数 n 。

输出格式

  输出多行,每行包含一个整数。

样例输入

123456789

样例输出

362880
2304
24
8

 两个循环,内层循环所用的数字就是外层循环的结果

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        while (n >= 10) {
            int res = 1;
            while (n != 0) {
                int digit = n % 10;
                if (digit != 0) {
                    res *= digit;
                }
                n /= 10;
            }
            System.out.println(res);
            n = res;
        }
    }
}

10.

问题描述

  小蓝站在一个 n 行 m 列的方格图中间,方格图的每一个方格上都标有一个正整数。
  如果两个相邻方格(上下左右四个方向相邻)内的数的最大公约数大于 1 ,则可以从其中一个方格移动到另一个方格,当然也可以从另一个方格移回第一个方格。
  假设小蓝开始时站在第 r 行第 c 列,请问小蓝可以移动到方格图内的多少个方格?

输入格式

  输入的第一行包含两个整数 n, m ,用一个空格分隔,表示方格图的行数和列数。
  接下来 n 行,每行包含 m 个正整数,相邻整数间用一个空格分隔,依次表示方格图中从第 1 行到第 n 行,每行从第 1 列到第 m 列中的数。
  接下来一行包含两个整数 r, c,用一个空格分隔,表示小蓝所在的行号和列号。

输出格式

  输出一行包含一个整数,表示答案。

样例输入

3 4
3 6 5 5
2 4 3 5
7 8 3 8
3 2

样例输出

5

这题用bfs会轻松一点,下面是我写的代码,还是仅供参考

package MATH.lanQiao;


import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class mod {
    static int[] dx = {0, 1, 0, -1};
    static int[] dy = {1, 0, -1, 0};
    static int count = 1;

    static class point {
        int x;
        int y;

        public point(int x, int y) {//构造器
            this.x = x;
            this.y = y;
        }
    }

    static Queue<point> queue = new LinkedList<>();//申请内存空间

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int row = sc.nextInt();
        int col = sc.nextInt();
        int[][] nums = new int[row][col];
        boolean[][] visited = new boolean[row][col];

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                nums[i][j] = sc.nextInt();
            }
        }
        //起始点
        int r = sc.nextInt() - 1;
        int c = sc.nextInt() - 1;

        point start = new point(r, c);
        queue.add(start);//入队
        visited[r][c] = true;

        while (!queue.isEmpty()) {
            //取出队首元素
            point p1 = queue.poll();
            int x = p1.x;
            int y = p1.y;

            for (int i = 0; i < 4; i++) {
                int nextX = x + dx[i];
                int nextY = y + dy[i];
                //尝试移动
                if (OutOfBounds(nextX, nextY, nums)
                        && gcd(nums[x][y], nums[nextX][nextY]) > 1
                        && !visited[nextX][nextY]) {
                    //没有越界的情况下公约数大于1
                    visited[nextX][nextY] = true;
                    point newP = new point(nextX, nextY);
                    queue.add(newP);//入队
                    count++;
                }
            }
        }
        System.out.println(count);
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else return gcd(b, a % b);
    }

    public static boolean OutOfBounds(int x, int y, int[][] map) {
        return x >= 0 && x < map.length && y >= 0 && y < map[0].length;
    }
}

这次模拟赛的题目难度不大,我都是暴力做的。

 

 

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年12月8日
下一篇 2023年12月8日

相关推荐