zoukankan      html  css  js  c++  java
  • 第六届蓝桥杯JavaB组国(决)赛真题

    解题代码部分来自网友,如果有不对的地方,欢迎各位大佬评论

    题目1、分机号

    X老板脾气古怪,他们公司的电话分机号都是3位数,老板规定,所有号码必须是降序排列,且不能有重复的数位。比如:

    751,520,321 都满足要求,而,
    766,918,201 就不符合要求。

    现在请你计算一下,按照这样的规定,一共有多少个可用的3位分机号码?

    请直接提交该数字,不要填写任何多余的内容。

    答案:120

    public class Main {
        
        public static void main(String[] args) {
            int count = 0;
            for(int a = 0;a < 10;a++)
                for(int b = 0;b < 10;b++)
                    for(int c = 0;c < 10;c++)
                        if(a > b && b > c)
                            count++;
            System.out.println(count);
        }
    }
    
    题目2、五星填数

    如【图1.png】的五星图案节点填上数字:1~12,除去7和11。
    要求每条直线上数字和相等。

    如图就是恰当的填法。

    请你利用计算机搜索所有可能的填法有多少种。
    注意:旋转或镜像后相同的算同一种填法。

    请提交表示方案数目的整数,不要填写任何其它内容。

    答案:12

    在这里插入图片描述

    public class Main {
        public static int count = 0;
        
        public void swap(int[] A, int i, int j) {
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
        }
        
        public void check(int[] A) {
            int sum1 = A[0] + A[2] + A[5] + A[8];
            int sum2 = A[0] + A[3] + A[6] + A[9];
            int sum3 = A[1] + A[2] + A[3] + A[4];
            int sum4 = A[1] + A[5] + A[7] + A[9];
            int sum5 = A[4] + A[6] + A[7] + A[8];
            if(sum1 == sum2 && sum1 == sum3 && sum1 == sum4 && sum1 == sum5) {
                count++;
            } else
                return;
        }
        
        public void dfs(int[] A, int step) {
            if(step == A.length) {
                check(A);
                return;
            } else {
                for(int i = step;i < A.length;i++) {
                    swap(A, i, step);
                    dfs(A, step + 1);
                    swap(A, i, step);
                }
            }
        }
        
        public static void main(String[] args) {
            Main test = new Main();
            int[] A = {1,2,3,4,5,6,8,9,10,12};
            test.dfs(A, 0);
            System.out.println(count / 10);
        }
    }
    
    题目3、显示二叉树
    题目描述
    排序二叉树的特征是:
    某个节点的左子树的所有节点值都不大于本节点值。
    某个节点的右子树的所有节点值都不小于本节点值。
    
    为了能形象地观察二叉树的建立过程,小明写了一段程序来显示出二叉树的结构来。
    
    
    class BiTree
    {
        private int v;
        private BiTree l;
        private BiTree r;
        
        public BiTree(int v){
            this.v = v;
        }
        
        public void add(BiTree the){
            if(the.v < v){
                if(l==null) l = the;
                else l.add(the);
            }
            else{
                if(r==null) r = the;
                else r.add(the);
            }
        }
        
        public int getHeight(){
            int h = 2;
            int hl = l==null? 0 : l.getHeight();
            int hr = r==null? 0 : r.getHeight();
            return h + Math.max(hl,hr);
        }
        
        public int getWidth(){
            int w = (""+v).length();
            if(l!=null) w += l.getWidth();
            if(r!=null) w += r.getWidth();
            return w;
        }
        
        public void show(){
            char[][] buf = new char[getHeight()][getWidth()];
            printInBuf(buf, 0, 0);
            showBuf(buf);
        }
        
        private void showBuf(char[][] x){
            for(int i=0; i<x.length; i++){
                for(int j=0; j<x[i].length; j++)
                    System.out.print(x[i][j]==0? ' ':x[i][j]);
                System.out.println();
            }
        }
        
        private void printInBuf(char[][] buf, int x, int y){
            String sv = "" + v;
            
            int p1 = l==null? x : l.getRootPos(x);
            int p2 = getRootPos(x);
            int p3 = r==null? p2 : r.getRootPos(p2+sv.length());
            
            buf[y][p2] = '|';
            for(int i=p1; i<=p3; i++) buf[y+1][i]='-';
            for(int i=0; i<sv.length(); i++) ________________________________;  //填空位置
            if(p1<p2) buf[y+1][p1] = '/';
            if(p3>p2) buf[y+1][p3] = '\';
            
            if(l!=null) l.printInBuf(buf,x,y+2);
            if(r!=null) r.printInBuf(buf,p2+sv.length(),y+2);
        }
        
        private int getRootPos(int x){
            return l==null? x : x + l.getWidth();
        }
    }
    
    public class Main
    {
        public static void main(String[] args)
        {        
            BiTree tree = new BiTree(500);
            tree.add(new BiTree(200));
            tree.add(new BiTree(509));
            tree.add(new BiTree(100));
            tree.add(new BiTree(250));
            tree.add(new BiTree(507));
            tree.add(new BiTree(600));
            tree.add(new BiTree(650));
            tree.add(new BiTree(450));
            tree.add(new BiTree(510));
            tree.add(new BiTree(440));
            tree.add(new BiTree(220));        
            tree.show();        
        }
    }
    
    对于上边的测试数据,应该显示出:
                      |
       /--------------500---
       |                    |
    /--200---           /--509---
    |        |           |        |
    100   /--250---     507   /--600
          |        |           |     |
          220   /--450         510   650
                |
                440
    
    (如有对齐问题,请参考【图1.png】)
    请分析程序逻辑,填写划线部分缺失的代码。
    注意,只填写缺少的部分,不要填写已有的代码或符号,也不要加任何说明文字。
    
    
    

    在这里插入图片描述

    // 先把要填代码的那行注释,运行一下,根据结果就知道buf第一个索引是y+1,并且是要将sv这个字符串填到buf中,然后第二个呢,试出来的哈哈哈
    buf[y + 1][p2+i] = sv.charAt(i);
    
    题目4、穿越雷区
    题目描述
    X星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废。
    某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能路径最短?
    
    已知的地图是一个方阵,上面用字母标出了A,B区,其它区都标了正号或负号分别表示正负能量辐射区。
    例如:
    A + - + -
    - + - - +
    - + + + -
    + - + - +
    B + - + -
    
    坦克车只能水平或垂直方向上移动到相邻的区。
    
    数据格式要求:
    
    输入第一行是一个整数n,表示方阵的大小, 4<=n<100
    接下来是n行,每行有n个数据,可能是A,B,+,-中的某一个,中间用空格分开。
    A,B都只出现一次。
    
    要求输出一个整数,表示坦克从A区到B区的最少移动步数。
    如果没有方案,则输出-1
    
    例如:
    用户输入:
    5
    A + - + -
    - + - - +
    - + + + -
    + - + - +
    B + - + -
    
    则程序应该输出:
    10
    
    资源约定:
    峰值内存消耗(含虚拟机) < 512M
    CPU消耗  < 2000ms
    
    
    请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
    
    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
    注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
    注意:主类的名字必须是:Main,否则按无效代码处理。
    
    
    // 深搜
    import java.util.Scanner;
     
    public class Main {
    	// 下一步的方向,分别为:上,下,左,右
    	static int[][] dir = { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 } };
    	// 答案
    	static int ans = Integer.MAX_VALUE;
     
    	public static void main(String[] args) {
    		Scanner s = new Scanner(System.in);
    		int n = s.nextInt();
    		String[][] map = new String[n][n];
    		// 1代表访问过,0代表未访问过
    		int[][] visit = new int[n][n];
    		// A的行号
    		int x = 0;
    		// A的列号
    		int y = 0;
    		for (int i = 0; i < map.length; i++) {
    			for (int j = 0; j < map[i].length; j++) {
    				map[i][j] = s.next();
    				if (map[i][j].equals("A")) {
    					x = i;
    					y = j;
    				}
    			}
    		}
    		dfs(map, visit, x, y, 0);
    		System.out.println(ans);
    		s.close();
    	}
     
    	public static void dfs(String[][] map, int[][] visit, int row, int col, int step) {
    		if (map[row][col].equals("B")) {
    			if (step < ans)
    				ans = step;
    			return;
    		}
    		visit[row][col] = 1;
    		for (int i = 0; i < dir.length; i++) {
    			// 下一步行号
    			int nextRow = row + dir[i][0];
    			// 下一步列号
    			int nextCol = col + dir[i][1];
    			if (nextRow < 0 || nextRow >= map.length || nextCol < 0 || nextCol >= map.length)
    				continue;
    			if (visit[nextRow][nextCol] == 0) {
    				visit[nextRow][nextCol] = 1;
    				if (!map[nextRow][nextCol].equals(map[row][col]))
    					dfs(map, visit, nextRow, nextCol, step + 1);
    				// 回溯
    				visit[nextRow][nextCol] = 0;
    			}
    		}
    	}
    }
    
    题目5、表格计算

    某次无聊中, atm 发现了一个很老的程序。这个程序的功能类似于 Excel ,它对一个表格进行操作。
    不妨设表格有 n 行,每行有 m 个格子。
    每个格子的内容可以是一个正整数,也可以是一个公式。
    公式包括三种:

    1. SUM(x1,y1:x2,y2) 表示求左上角是第 x1 行第 y1 个格子,右下角是第 x2 行第 y2 个格子这个矩形内所有格子的值的和。
    2. AVG(x1,y1:x2,y2) 表示求左上角是第 x1 行第 y1 个格子,右下角是第 x2 行第 y2 个格子这个矩形内所有格子的值的平均数。
    3. STD(x1,y1:x2,y2) 表示求左上角是第 x1 行第 y1 个格子,右下角是第 x2 行第 y2 个格子这个矩形内所有格子的值的标准差。

    标准差即为方差的平方根。
    方差就是:每个数据与平均值的差的平方的平均值,用来衡量单个数据离开平均数的程度。

    公式都不会出现嵌套。

    如果这个格子内是一个数,则这个格子的值等于这个数,否则这个格子的值等于格子公式求值结果。

    输入这个表格后,程序会输出每个格子的值。atm 觉得这个程序很好玩,他也想实现一下这个程序。

    「输入格式」
    第一行两个数 n, m 。
    接下来 n 行输入一个表格。每行 m 个由空格隔开的字符串,分别表示对应格子的内容。
    输入保证不会出现循环依赖的情况,即不会出现两个格子 a 和 b 使得 a 的值依赖 b 的值且 b 的值依赖 a 的值。

    「输出格式」
    输出一个表格,共 n 行,每行 m 个保留两位小数的实数。
    数据保证不会有格子的值超过 1e6 。

    「样例输入」
    3 2
    1 SUM(2,1:3,1)
    2 AVG(1,1:1,2)
    SUM(1,1:2,1) STD(1,1:2,2)

    「样例输出」
    1.00 5.00
    2.00 3.00
    3.00 1.48

    「数据范围」
    对于 30% 的数据,满足: n, m <= 5
    对于 100% 的数据,满足: n, m <= 50

    资源约定:
    峰值内存消耗(含虚拟机) < 512M
    CPU消耗 < 2000ms

    请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。

    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
    注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
    注意:主类的名字必须是:Main,否则按无效代码处理。

    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Main {
        public static int n, m;
        public static double[][] value;
        
        public double getSum(int x1, int y1, int x2, int y2) {
            double sum = 0;
            for(int i = x1;i <= x2;i++)
                for(int j = y1;j <= y2;j++)
                    sum = sum + value[i][j];
            return sum;
        }
        
        public double getAvg(int x1, int y1, int x2, int y2) {
            int count = Math.abs((x2 - x1 + 1) * (y2 - y1 + 1));
            double avg = getSum(x1, y1, x2, y2) / count;
            return avg;
        }
        
        public double getStd(int x1, int y1, int x2, int y2) {
            int count = Math.abs((x2 - x1 + 1) * (y2 - y1 + 1));
            double avg = getAvg(x1, y1, x2, y2);
            double result = 0;
            for(int i = x1;i <= x2;i++)
                for(int j = y1;j <= y2;j++)
                    result = result + (value[i][j]-avg) * (value[i][j]-avg);
            result = Math.sqrt(result / count);
            return result;
        }
        
        public boolean check(int x1, int y1, int x2, int y2) {
            boolean judge = true;
            for(int i = x1;i <= x2;i++) {
                if(!judge)
                    break;
                for(int j = y2;j <= y2;j++) {
                    if(value[i][j] == -1) {
                        judge = false;
                        break;
                    }
                }
            }
            return judge;
        }
        
        public String[] getOperaAndNum(String arrayA) {
            int p = arrayA.indexOf("(");
            int q = arrayA.indexOf(")");
            String opera = arrayA.substring(0, p);
            arrayA = arrayA.replace(':', ',');
            String[] num = arrayA.substring(p+1, q).split(",");
            String[] result = new String[5];
            result[0] = opera;
            for(int i = 0;i < 4;i++)
                result[i + 1] = num[i];
            return result;
        }
        
        public void getResult(String[] A) {
            value = new double[n][m];
            ArrayList<String> list = new ArrayList<String>();
            for(int i = 0;i < n;i++)
                for(int j = 0;j < m;j++) 
                    value[i][j] = -1;
            for(int i = 0;i < A.length;i++) {
                String[] arrayA = A[i].split(" ");
                for(int j = 0;j < arrayA.length;j++) {
                    if(arrayA[j].charAt(0) >= '0' && arrayA[j].charAt(0) <= '9') {
                        value[i][j] = Double.valueOf(arrayA[j]);
                    } else {
                        String[] r = getOperaAndNum(arrayA[j]);
                        String opera = r[0];
                        int x1 = Integer.valueOf(r[1]) - 1;
                        int y1 = Integer.valueOf(r[2]) - 1;
                        int x2 = Integer.valueOf(r[3]) - 1;
                        int y2 = Integer.valueOf(r[4]) - 1;
                        if(check(x1, y1, x2, y2) == false) {
                            list.add(""+i+" "+j+" "+arrayA[j]);
                            continue;
                        }
                        if(opera.equals("SUM"))
                            value[i][j] = getSum(x1, y1, x2, y2);
                        else if(opera.equals("AVG"))
                            value[i][j] = getAvg(x1, y1, x2, y2);
                        else if(opera.equals("STD"))
                            value[i][j] = getStd(x1, y1, x2, y2);
                    }
                }
            }
        
            while(!list.isEmpty()) {
                for(int i = list.size() - 1;i >= 0;i--) {
                    String[] temp = list.get(i).split(" ");
                    int a = Integer.valueOf(temp[0]);
                    int b = Integer.valueOf(temp[1]);
                    String[] r = getOperaAndNum(temp[2]);
                    String opera = r[0];
                    int x1 = Integer.valueOf(r[1]) - 1;
                    int y1 = Integer.valueOf(r[2]) - 1;
                    int x2 = Integer.valueOf(r[3]) - 1;
                    int y2 = Integer.valueOf(r[4]) - 1;
                    if(check(x1, y1, x2, y2) == false) 
                        continue;
                    if(opera.equals("SUM"))
                        value[a][b] = getSum(x1, y1, x2, y2);
                    else if(opera.equals("AVG"))
                        value[a][b] = getAvg(x1, y1, x2, y2);
                    else if(opera.equals("STD"))
                        value[a][b] = getStd(x1, y1, x2, y2);
                    list.remove(i);
                }
            }
            for(int i = 0;i < n;i++) {
                for(int j = 0;j < m;j++) {
                    System.out.printf("%.2f", value[i][j]);
                    if(j != m - 1)
                        System.out.print(" ");
                }
                System.out.println();
            }
        }
        
        public static void main(String[] args) {
            Main test = new Main();
            Scanner in = new Scanner(System.in);
            n = in.nextInt();
            m = in.nextInt();
            in.nextLine();
            String[] A = new String[n];
            for(int i = 0;i < n;i++)
                A[i] = in.nextLine();
            test.getResult(A);
        }
    }
    
    题目6、铺瓷砖

    题目描述
    为了让蓝桥杯竞赛更顺利的进行,主办方决定给竞赛的机房重新铺放瓷砖。机房可以看成一个n*m的矩形,而这次使用的瓷砖比较特别,有两种形状,如【图1.png】所示。在铺放瓷砖时,可以旋转。

    主办方想知道,如果使用这两种瓷砖把机房铺满,有多少种方案。

    【输入格式】
    输入的第一行包含两个整数,分别表示机房两个方向的长度。

    【输出格式】
    输出一个整数,表示可行的方案数。这个数可能很大,请输出这个数除以65521的余数。

    【样例输入1】
    4 4
    【样例输出1】
    2
    【样例说明1】
    这两种方案如下【图2.png】所示:

    【样例输入2】
    2 6
    【样例输出2】
    4
    【数据规模与约定】
    对于20%的数据,1<=n, m<=5。
    对于50%的数据,1<=n<=100,1<=m<=5。
    对于100%的数据,1<=n<=10^15,1<=m<=6。

    资源约定:
    峰值内存消耗(含虚拟机) < 512M
    CPU消耗 < 8000ms

    请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。

    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
    注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
    注意:主类的名字必须是:Main,否则按无效代码处理。
    在这里插入图片描述
    在这里插入图片描述

    小编能力有限,还望大佬解决

  • 相关阅读:
    Atitit  atiMail atiDns新特性 v2  q39
    Atitit  atiMail atiDns新特性 v2  q39
    Atitit.aticmd v4  新特性q39 添加定时器释放功能
    Atitit.aticmd v4  新特性q39 添加定时器释放功能
    Atitit. Atiposter 发帖机 新特性 poster new feature   v7 q39
    Atitit. Atiposter 发帖机 新特性 poster new feature   v7 q39
    Atitit.编程语言and 自然语言的比较and 编程语言未来的发展
    Atitit.编程语言and 自然语言的比较and 编程语言未来的发展
    atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException  v2 q31
    知也atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException  v2 q31无涯 - I
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13078661.html
Copyright © 2011-2022 走看看