学习笔记·第十二届蓝桥杯第二期校内模拟赛·Java赛道

刚考完,做个记录。答案都是自己做得,不保证正确性
DFS是什么? BFS是什么?傻傻不知道

结果题

无向图

  • 问题描述
    一个无向图包含2020条边,如果图中没有自环和重边,请问最少包含多少个结点?

  • 我的答案

    2039190

  • 解题思路

    n*(n-1)/2

排列组合一

  • 问题描述
    请问有多少个序列满足下面的条件:

    1. 序列的长度为5
    2. 序列中的每个数都是从1到10之间的整数
    3. 序列中后面的数大于等于前面的数
  • 我的答案

    2002

  • 解题思路

public static final int max = 10;
    public static void main(String[] args) {
//        int number = 255;
//        System.out.println(Integer.toBinaryString(number).length());
        int[] numbers = {1,1,1,1,1};
        int count = 0;
        while (numbers[0] < max && numbers[1] < max &&numbers[2] < max &&numbers[3] < max &&numbers[4] < max ){
            for(int i = 1;i<=max;i++){
                numbers[0] = i;
                for(int j = 1;j<=max;j++){
                    if (j>=i) {
                        numbers[1] = j;
                        for (int k = 1;k<=max;k++){
                            if(k>=j) {
                                numbers[2] = k;
                                for (int l = 1;l<=max;l++){
                                    if(l>=k) {
                                        numbers[3] = l;
                                        for (int m = 1;m<=max;m++){
                                            if (m>=l) {
                                                numbers[4] = m;
                                                count ++;
                                                System.out.println(Arrays.toString(numbers));
                                            }
                                        }
                                    }

                                }
                            }

                        }
                    }
                }
            }
        }
        System.out.println(count);
    }

排列组合二

  • 问题描述
    请问有多少个单词满足下面的条件:
    1. 单词的长度至少为
    2. 单词中的字母全部由大写字母A-G组成
    3. 单词中的每个字母严格大于其左边的字母
  • 我的答案

127

  • 解题思路

基本与上一道题相似,稍微修改下边界条件即可

二进制转换一

  • 问题描述
    小明要用二进制来表示1到10000的所有整数,要求不同的整数用不同的二进制数表示,请问,为了表示1到10000的所有整数,至少需要多少个二进制位?
  • 我的答案

14

  • 解题思路

    将10000用二进制数显示即是最少的二进制位

    public static void main(String[] args) {
        int number = 10000;
        System.out.println(Integer.toBinaryString(number).length());
    }
    控制台输出:14
    

二进制转换二

  • 问题描述
    一个IPv4的地址是由点分十进制数表示的,例如 192.168.20.20。
    在网络传输时,一般直接把IPv4地址的数值部分表示成二进制数在网络中传输。请问,在网络中传输一个IPv4地址需要多少个二进制位?
  • 我的答案

32

  • 解题思路

由常识可得该题答案为32位(呸~)
一个点分十进制最大为255,由Java可得一个最多为8位,四个即32位

public static void main(String[] args) {
        int number = 255;
        System.out.println(Integer.toBinaryString(number).length());
    }
    控制台输出:8

编程题

求面积 一

  • 问题描述
    给定一个平行四边形的底边长度l和高度h,求平行四边形的面积。
  • 输入格式
    输入的第一行包含一个整数 l,表示平行四边形的底边长度。
    第二行包含一个整数 h,表示平行四边形的高
  • 输出格式
    输出一个整数,表示平行四边形的面积。(提示:底边长度和高都是整数的平行四边形面积为整数)
  • 我的答案
     public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
            int l=input.nextInt();
            int h = input.nextInt();
            System.out.println(l*h);
        }
    

求面积 二

  • 问题描述
    给定一个梯形的两条底边长度a,b和高度h,求梯形的面积。

  • 输入格式
    输入的第一行包含两个整数a,b 表示梯形的两条底边长度。
    第二行包含一个整数h,表示梯形的高。

  • 输出格式
    输出一个属,表示梯形的面积。如果面积为整数,请直接输出这个整数,不带小数点,如果面积不是整数,请四舍五入保留正好一位小数。

  • 我的答案

     public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int a,b,h;
            a = scanner.nextInt();
            b = scanner.nextInt();
            int temp = a+b;//上底加下底
            h = scanner.nextInt();
            DecimalFormat df=new DecimalFormat("0.0");//设置保留位数
            if (temp*h%2 == 0) System.out.println(temp*h/2);
            else System.out.println(df.format((float)temp*h/2));
        }
    

求时间

  • 问题描述
    现在时间是a点b分,请问t分钟后,是几点几分?

  • 输入格式
    输入的第一行包含一个整数a
    第二行包含整数b
    第三行包含整数t

  • 输出格式
    输出第一行包含一个整数,表示结果是几点
    第二行包含一个整数,表示结果是几分

  • 我的答案

     public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int hour = scanner.nextInt();
            int min = scanner.nextInt();
            int next = scanner.nextInt();
    
    
            int nextHour = next / 60;
            int nextMin = next % 60;
            if(min + nextMin > 60) {
                nextHour = nextHour + 1;
            }
            nextMin =(min + nextMin)%60;
            System.out.println(hour + nextHour);
            System.out.println(nextMin);
        }
    

水灌溉问题

  • 问题描述

    小蓝负责花园的灌溉工作。

    花园可以看成一个n行m列的方格图形。中间有一部分位置上安装有出水管。

    小蓝可以控制一一个按钮同时打开所有的出水管,打开时,有出水管的位置可以被认为已经灌溉好。

    每经过一分钟, 水就会向四面扩展一个方格, 被扩展到的方格可以被认为已经灌溉好。即如果前一分钟某-个方格被灌溉好,则下一分钟它上下左右的四个方格也被灌溉好。

    给定花园水管的位置,请问k分钟后,有多少个方格被灌溉好?
    MC直呼内涵

  • 输入格式

    输入的第一行包含两个整数n, m。

    第二行包含一个整数t,表示出水管的数量。

    接下来t行描述出水管的位置,其中第i行包含两个数r, C表示第r行第c列有一个排水管。

    接下来一行包含一个整数k

  • 输出格式
    输出一个整数,表示答案。

  • 样例输入
    3 6
    2
    2 2
    3 4
    1

  • 样例输出
    9

  • 我的答案

public static void main(String[] args) {
        int[][] pipePos; //0保存水管水平 1保存垂直位置
        int[][] block;  //方块田
        Scanner scanner = new Scanner(System.in);
        int rows = scanner.nextInt();//行数
        int cols = scanner.nextInt();//列数
        block = new int[rows][cols]; //新建方块田
        int pipe = scanner.nextInt();//水管数量
        pipePos = new int[2][100000];

        for (int i = 0; i < pipe; i++) {
            int row = scanner.nextInt();//水管水平位置
            int col = scanner.nextInt();//水管垂直位置
            pipePos[0][i] = row;
            pipePos[1][i] = col;
            block[row - 1][col - 1] = 1;//替换水管方块
        }

        int time = scanner.nextInt();//过去几分钟


        for (int i = 0; i < time; i++) {  //n分钟n次
            int temp = 0;
            for (int j = 0; j < pipe; j++) { //几条水管遍历几次
                if (pipePos[0][j] - 2 >= 0 && block[pipePos[0][j] - 2][pipePos[1][j] - 1] == 0) { //上方格子
                    pipePos[0][pipe + temp] = pipePos[0][j] - 1;
                    pipePos[1][pipe + temp] = pipePos[1][j];
                    block[pipePos[0][j] - 2][pipePos[1][j] - 1] = 1;
                    temp++; //新增水管
                }
                if (pipePos[0][j] < rows && block[pipePos[0][j]][pipePos[1][j] - 1] == 0) { //下方格子
                    pipePos[0][pipe + temp] = pipePos[0][j] + 1;
                    pipePos[1][pipe + temp] = pipePos[1][j];
                    block[pipePos[0][j]][pipePos[1][j] - 1] = 1;
                    temp++; //新增水管
                }
                if (pipePos[1][j] - 2 >= 0 && block[pipePos[0][j] - 1][pipePos[1][j] - 2] == 0) { //左边
                    pipePos[0][pipe + temp] = pipePos[0][j];
                    pipePos[1][pipe + temp] = pipePos[1][j] - 1;
                    block[pipePos[0][j] - 1][pipePos[1][j] - 2] = 1;
                    temp++; //新增水管
                }
                if (pipePos[1][j] < cols && block[pipePos[0][j] - 1][pipePos[1][j]] == 0) { //右边
                    pipePos[0][pipe + temp] = pipePos[0][j];
                    pipePos[1][pipe + temp] = pipePos[1][j] + 1;
                    block[pipePos[0][j] - 1][pipePos[1][j]] = 1;
                    temp++; //新增水管
                }
            }
            pipe = pipe + temp;
        }
        int count = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (block[i][j] == 1) count++;
            }
        }
        System.out.println(count);
//
//        for (int i = 0; i < rows; i++) { //调试输出
//            System.out.println(Arrays.toString(block[i]));
//        }
//
//        for (int i = 0; i < pipe; i++) {
//            System.out.print(pipePos[0][i]+"----");
//            System.out.println(pipePos[1][i]);
//        }
//
//    }

    }

均值滤波

  • 问题描述

    小蓝有一张黑白图像,由n* m个像素组成,其中从上到下共n行,每行从左到右m列。每个像素由一个0到255之间的灰度值表示。

    现在,小蓝准备对图像进行模糊操作,操作的方法为:

    对于每个像素,将以它为中心3* 3区域内的所有像素(可能是9个像素或少于9个像素)求和后除以这个范围内的像素个数(取下整),得到的值就是模糊后的结果。

    请注意每个像素都要用原图中的灰度值计算求和。

  • 输入格式

    输入的第一行包含两个整数n, m。

    第2行到第n+ 1行每行包含m个整数,表示每个像素的灰度值,相邻整数之间用一个空格分隔。

  • 输出格式

    输出n行,每行m个整数,相邻整数之间用空格分隔,示模糊后的图像。

  • 样例输入
    3 4
    0 0 0 255
    0 0 255 0
    0 30 255 255

  • 样例输出
    0 42 85 127
    5 60 116 170
    7 90 132 191

  • 我的答案

 public static void main(String[] args) {
        int[][] image;
        int[][] imageFilter;
        Scanner scanner = new Scanner(System.in);
        int rows = scanner.nextInt();
        int cols = scanner.nextInt();

        image = new int[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                image[i][j]  = scanner.nextInt();
            }
        }

        imageFilter = new int[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                int around = 1;
                int count = image[i][j];
                //判断它周围有多少个方块
                //top
                if (i-1 >= 0) {
                    count = count + image[i-1][j];
                    around = around +1;
                }
                //bottom
                if (i+1 <rows) {
                    count = count + image[i+1][j];
                    around = around +1;
                }
                //left
                if (j-1 >= 0) {
                    count = count + image[i][j-1];
                    around = around +1;
                }
                //right
                if (j+1 < cols) {
                    count = count + image[i][j+1];
                    around = around +1;
                }
                //top-left
                if (i-1>=0 && j-1>=0){
                    count = count + image[i-1][j-1];
                    around = around +1;
                }
                //top-right
                if (i-1>=0 && j+1<cols) {
                    count = count + image[i-1][j+1];
                    around = around +1;
                }
                //bottom-left
                if (i+1<rows&& j-1>=0) {
                    count = count + image[i+1][j-1];
                    around = around +1;
                }
                //bottom-right
                if (i+1<rows && j+1<cols) {
                    around = around +1;
                    count = count + image[i+1][j+1];
                }

                imageFilter[i][j] = count/around;
            }
        }


        for (int i = 0; i < rows; i++) { //调试输出
            for (int j = 0; j <cols ; j++) {
                if (j < cols-1) System.out.print(imageFilter[i][j]+" ");
                else System.out.print(imageFilter[i][j]);
            }
            if(i<rows-1) System.out.print("\n");
        }
    }

扫雷问题

  • 问题描述
    在一个n行m列的方格图上有一些位置有地雷,另外一些位置为空。

    请为每个空位置标一个整数,表示周围八个相邻的方格中有多少个地雷。

  • 输入格式

    输入的第一行包含两个整数n, m。

    第2行到第n+ 1行每行包含m个整数,相邻整数之间用一个空格分隔。如果对应的整数为0,表示这一格没有地雷。如果对应的整数为1,表示这一格有地雷。

  • 输出格式

    输出n行,每行m个整数,相邻整数之间用空格分隔。

    对于没有地雷的方格,输出这格周围的地雷数量。对于有地雷的方格,输出9。

  • 样例输入
    3 4
    0 1 0 0
    1 0 1 0
    0 0 1 0

  • 样例输出
    2 9 2 1
    9 4 9 2
    1 3 9 2

  • 我的答案

public static void main(String[] args) {
        int[][] boom;
        int[][] boomHandle;
        Scanner scanner = new Scanner(System.in);
        int rows = scanner.nextInt();
        int cols = scanner.nextInt();

        boom = new int[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                boom[i][j] = scanner.nextInt();
            }
        }

        boomHandle = new int[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (boom[i][j] == 1) {
                    boomHandle[i][j] = 9;
                    continue;
                }
                int around = 0;

                //判断它周围有多少个雷
                //top
                if (i - 1 >= 0 && boom[i - 1][j] == 1) {
                    around = around + 1;
                }
                //bottom
                if (i + 1 < rows && boom[i + 1][j] == 1) {
                    around = around + 1;
                }
                //left
                if (j - 1 >= 0 && boom[i][j - 1] == 1) {
                    around = around + 1;
                }
                //right
                if (j + 1 < cols && boom[i][j + 1] == 1) {
                    around = around + 1;
                }
                //top-left
                if (i - 1 >= 0 && j - 1 >= 0 && boom[i - 1][j - 1] == 1) {
                    around = around + 1;
                }
                //top-right
                if (i - 1 >= 0 && j + 1 < cols && boom[i - 1][j + 1] == 1) {
                    around = around + 1;
                }
                //bottom-left
                if (i + 1 < rows && j - 1 >= 0 && boom[i + 1][j - 1] == 1) {
                    around = around + 1;
                }
                //bottom-right
                if (i + 1 < rows && j + 1 < cols && boom[i + 1][j + 1] == 1) {
                    around = around + 1;
                }

                boomHandle[i][j] = around;
            }
        }


        for (int i = 0; i < rows; i++) { //调试输出
            for (int j = 0; j < cols; j++) {
                if (j < cols - 1) System.out.print(boomHandle[i][j] + " ");
                else System.out.print(boomHandle[i][j]);
            }
            if (i < rows - 1) System.out.print("\n");
        }
    }

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值