zoukankan      html  css  js  c++  java
  • 算法笔记_208:第六届蓝桥杯软件类决赛真题(Java语言A组)

    目录

    1 胡同门牌号

    2 四阶幻方

    3 显示二叉树

    4 穿越雷区

    5 切开字符串

    6 铺瓷砖

     

      前言:以下代码仅供参考,若有错误欢迎指正哦~


    1 胡同门牌号

    标题:胡同门牌号
    
    小明家住在一条胡同里。胡同里的门牌号都是连续的正整数,由于历史原因,最小的号码并不是从1开始排的。
    有一天小明突然发现了有趣的事情:
    如果除去小明家不算,胡同里的其它门牌号加起来,刚好是100!
    并且,小明家的门牌号刚好等于胡同里其它住户的个数!
    
    请你根据这些信息,推算小明家的门牌号是多少?
    
    请提交该整数,不要填写任何多余的内容或说明性文字。
    
    运行结果有两个:8和10,但是针对此题,答案到底是8还是10还是8和10,不好判定。以下代码仅供参考。
    (1)8 
    (2)10
     1 public class Main {
     2     
     3     public static void main(String[] args) {
     4         for(int i = 2;i < 200;i++) {
     5             int sum = 0;
     6             int count = 0;
     7             for(int j = i;j < 200;j++) {
     8                 count++;
     9                 sum = sum + j;
    10                 if(sum - count + 1 == 100 && count - 1 >= i)
    11                     System.out.println("i = "+i+", j = "+j+", count = "+(count-1));
    12             }
    13         }
    14     }
    15 }

    2 四阶幻方

    标题:四阶幻方
    
    把1~16的数字填入4x4的方格中,使得行、列以
    
    及两个对角线的和都相等,满足这样的特征时称
    
    为:四阶幻方。
    
    四阶幻方可能有很多方案。如果固定左上角为1
    
    ,请计算一共有多少种方案。
    比如:
      1  2 15 16
     12 14  3  5
     13  7 10  4
      8 11  6  9
    
    以及:
      1 12 13  8
      2 14  7 11
     15  3 10  6
     16  5  4  9
     
    就可以算为两种不同的方案。
    
    请提交左上角固定为1时的所有方案数字,不要
    
    填写任何多余内容或说明文字。
    
    
    答案:416
     1 import java.util.ArrayList;
     2 
     3 public class Main {
     4     public static boolean[] used = new boolean[17];
     5     public static ArrayList<String> list = new ArrayList<String>();
     6     public static int count = 0;
     7     
     8     public boolean check(int[] A, int step) {
     9         if(step >= 4)
    10             if(A[0] + A[1] + A[2] + A[3] != 34)
    11                 return false;
    12         if(step >= 8)
    13             if(A[4] + A[5] + A[6] + A[7] != 34)
    14                 return false;
    15         if(step >= 12)
    16             if(A[8] + A[9] + A[10] + A[11] != 34)
    17                 return false;
    18         if(step >= 13)
    19             if(A[0] + A[4] + A[8] + A[12] != 34 || A[3] + A[6] + A[9] + A[12] != 34)
    20                 return false;
    21         if(step >= 14)
    22             if(A[1] + A[5] + A[9] + A[13] != 34)
    23                 return false;
    24         if(step >= 15)
    25             if(A[2] + A[6] + A[10] + A[14] != 34)
    26                 return false;
    27         if(step >= 16)
    28             if(A[3] + A[7] + A[11] + A[15] != 34 || A[0] + A[5] + A[10] + A[15] != 34)
    29                 return false;
    30         return true;
    31     }
    32     
    33     public void dfs(int[] A, int step) {
    34         if(check(A, step) == false)
    35             return;
    36         if(step == 16) {
    37             StringBuffer s = new StringBuffer("");
    38             for(int i = 0;i < A.length;i++)
    39                 s.append(A[i]);
    40             if(!list.contains(s.toString())) {
    41                 list.add(s.toString());
    42                 count++;
    43             }
    44             return;
    45         }
    46         for(int i = 2;i <= 16;i++) {
    47             if(used[i] == false) {
    48                 used[i] = true;
    49                 A[step] = i;
    50                 dfs(A, step + 1);
    51                 used[i] = false;
    52             }
    53         }
    54     }
    55     
    56     public static void main(String[] args) {
    57         Main test = new Main();
    58         int[] A = new int[16];
    59         A[0] = 1;
    60         used[1] = true;
    61         test.dfs(A, 1);
    62         System.out.println(count);
    63     }
    64 }

    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][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,否则按无效代码处理。
     1 import java.util.Scanner;
     2 
     3 public class Main {
     4     public static int min = 1000 * 1000;
     5     public static int n;
     6     public static String[][] map;
     7     public static int[][] move = {{-1,0},{1,0},{0,-1},{0,1}}; //表示分别向上、下、左、右移动一步
     8     
     9     public void dfs(boolean[][] visited, int x, int y, int count) {
    10         if(map[x][y].equals("B")) {
    11             min = Math.min(min, count);
    12             return;
    13         }
    14         for(int i = 0;i < 4;i++) {
    15             int tempX = x + move[i][0];
    16             int tempY = y + move[i][1];
    17             if(tempX >= 0 && tempX < n && tempY >= 0 && tempY < n) {
    18                 if(!map[x][y].equals(map[tempX][tempY]) && !visited[tempX][tempY]) {
    19                     visited[tempX][tempY] = true;
    20                     dfs(visited, tempX, tempY, count + 1);
    21                     visited[tempX][tempY] = false;
    22                 }
    23             }
    24         }
    25     }
    26     
    27     public void getResult() {
    28         int startX = 0, startY = 0;
    29         for(int i = 0;i < n;i++)
    30             for(int j = 0;j < n;j++)
    31                 if(map[i][j] == "A") {
    32                     startX = i;
    33                     startY = j;
    34                 }
    35         boolean[][] visited = new boolean[n][n];
    36         visited[startX][startY] = true;
    37         dfs(visited, startX, startY, 0);
    38         if(min == 1000 * 1000)
    39             System.out.println("-1");
    40         else
    41             System.out.println(min);
    42     }
    43     
    44     public static void main(String[] args) {
    45         Main test = new Main();
    46         Scanner in = new Scanner(System.in);
    47         n = in.nextInt();
    48         in.nextLine();
    49         String[] T = new String[n];
    50         map = new String[n][n];
    51         for(int i = 0;i < n;i++) {
    52             T[i] = in.nextLine();
    53             map[i] = T[i].split(" ");
    54         }
    55         test.getResult();
    56     }
    57 }

    5 切开字符串

    标题:切开字符串
    
    Pear有一个字符串,不过他希望把它切成两段。
    这是一个长度为N(<=10^5)的字符串。
    Pear希望选择一个位置,把字符串不重复不遗漏地切成两段,长度分别是t和N-t(这两段都必须非空)。
    
    Pear用如下方式评估切割的方案:
    定义“正回文子串”为:长度为奇数的回文子串。
    设切成的两段字符串中,前一段中有A个不相同的正回文子串,后一段中有B个不相同的非正回文子串,则该方案的得分为A*B。
    
    注意,后一段中的B表示的是:“...非正回文...”,而不是: “...正回文...”。
    那么所有的切割方案中,A*B的最大值是多少呢?
    
    【输入数据】
    输入第一行一个正整数N(<=10^5)
    接下来一行一个字符串,长度为N。该字符串仅包含小写英文字母。
    【输出数据】
    一行一个正整数,表示所求的A*B的最大值。
    【样例输入】
    10
    bbaaabcaba
    【样例输出】
    38
    【数据范围】
    对于20%的数据,N<=100
    对于40%的数据,N<=1000
    对于100%的数据,N<=10^5
    
    资源约定:
    峰值内存消耗(含虚拟机) < 512M
    CPU消耗  < 2000ms
    
    
    请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
    
    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
    注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
    注意:主类的名字必须是:Main,否则按无效代码处理。
    
    
    以下代码对于N大于10000可能会超时,代码仅供参考。
    import java.util.HashSet;
    import java.util.Scanner;
    
    public class Main {
        public static String A, S;
        
        public int Manacher(String M, String N) {
            int count = 0;  //统计字符串M中不同正回文串个数
            int[] P = new int[M.length()];
            int mx = 0, id = 0;
            for(int i = 1;i < M.length();i++) {
                if(mx > i)
                    P[i] = Math.min(P[2 * id - i], mx - i);
                else
                    P[i] = 1;
                while(i+P[i] < M.length() && i-P[i] >= 0 && 
                        M.charAt(i+P[i]) == M.charAt(i-P[i]))
                    P[i]++;
                if(P[i] + i > mx) {
                    mx = i + P[i];
                    id = i;
                }
            }
            HashSet<String> set = new HashSet<String>();
            for(int i = 2;i < P.length;i = i + 2) {
                int len = P[i] - 1;
                int j = i / 2 - 1;  //回文串中心字符在字符串N中的位置
                if(len == 0) {
                    String s = N.substring(j, j + 1);
                    if(!set.contains(s)) {
                        set.add(s);
                        count++;
                    }
                } else if(len % 2 == 1){
                    for(int k = 0;k <= len / 2;k++) {
                        String s = N.substring(j - k, j + k + 1);
                        if(!set.contains(s)) {
                            set.add(s);
                            count++;
                        }
                    }
                }
            }
            return count;
        }
        
        public int getAllChildern(String M) {
            int count = 0;  //统计字符串M所有不同子串个数
            HashSet<String> set = new HashSet<String>();
            for(int i = 0, len = 1;i < M.length();i++, len++) {
                for(int j = 0;j <= M.length() - len;j++) {
                    String s = M.substring(j, j + len);
                    if(!set.contains(s)) {
                        set.add(s);
                        count++;
                    }
                }
            }
            return count;
        }
        
        public void getResult() {
            int max = -1;
            for(int i = 1;i < A.length() - 1;i++) {
                String s1 = S.substring(0, i * 2 + 1);
                String s2 = "$" + S.substring(i * 2 + 1);
                String s3 = A.substring(0, i);
                String s4 = A.substring(i);
                int a = Manacher(s1, s3);
                int b = getAllChildern(s4) - Manacher(s2, s4);
                max = Math.max(max, a * b);
            }
            System.out.println(max);
        }
        
        public static void main(String[] args) {
            Main test = new Main();
            Scanner in = new Scanner(System.in);
            A = in.next();
            StringBuilder s1 = new StringBuilder("$#");
            for(int i = 0;i < A.length();i++) {
                s1.append(A.charAt(i));
                s1.append("#");
            }
            S = s1.toString();
            test.getResult();
        }
    }

    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,否则按无效代码处理。

                                  图1

                                            图2

    问题待解决>~<

  • 相关阅读:
    springboot成神之——websocket发送和请求消息
    springboot成神之——发送邮件
    springboot成神之——spring文件下载功能
    springboot成神之——spring的文件上传
    springboot成神之——basic auth和JWT验证结合
    springboot成神之——Basic Auth应用
    leetcode-easy-array-122 best time to buy and sell stocks II
    leetcode-easy-array-31 three sum
    leetcode-mid-others-621. Task Scheduler
    leetcode-mid-math-371. Sum of Two Integers-NO-???
  • 原文地址:https://www.cnblogs.com/liuzhen1995/p/6872742.html
Copyright © 2011-2022 走看看