• 算法笔记_202:第三届蓝桥杯软件类决赛真题(Java高职)


    目录

    1 填算式

    2 提取子串

    3 机器人行走

    4 地址格式转换

    5 排日程

     


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


    1 填算式

    【结果填空】 (满分11分)
    
        看这个算式:
     
        ☆☆☆ + ☆☆☆ = ☆☆☆
    
        如果每个五角星代表 1 ~ 9 的不同的数字。
    
        这个算式有多少种可能的正确填写方法?
    
        173 + 286 = 459
        295 + 173 = 468
        173 + 295 = 468
        183 + 492 = 675
    
        以上都是正确的填写法!
    
        注意:
        111 + 222 = 333 是错误的填写法!
        因为每个数字必须是不同的! 
        也就是说:1~9中的所有数字,每个必须出现且仅出现一次!
    
        注意:
        不包括数字“0”!
    
        注意:
        满足加法交换率的式子算两种不同的答案。
        所以答案肯定是个偶数!
    
        注意:
        只要求计算不同的填法的数目
        不要求列出所有填写法
        更不要求填写源代码!
    
        答案不要写在这里,请写在“解答.txt”中!
    
    
    336
     1 public class Main {
     2     public static int count = 0;
     3     
     4     public static void swap(int[] A, int i, int j) {
     5         int temp = A[i];
     6         A[i] = A[j];
     7         A[j] = temp;
     8     }
     9     
    10     public static void dfs(int[] A, int step) {
    11         if(step == A.length) {
    12             int a = A[0]*100 + A[1]*10 + A[2];
    13             int b = A[3]*100 + A[4]*10 + A[5];
    14             int c = A[6]*100 + A[7]*10 + A[8];
    15             if(a + b == c)
    16                 count++;
    17             return;
    18         } else {
    19             for(int i = step;i < A.length;i++) {
    20                 swap(A, i, step);
    21                 dfs(A, step + 1);
    22                 swap(A, i, step);
    23             }
    24         }
    25     }
    26 
    27     public static void main(String[] args) {
    28         int[] A = {1,2,3,4,5,6,7,8,9};
    29         dfs(A, 0);
    30         System.out.println(count);
    31     }
    32 }

    2 提取子串

    【代码填空】(满分16分)
        
        串“abcba”以字母“c”为中心左右对称;串“abba” 是另一种模式的左右对称。这两种情况我们都称这个串是镜像串。特别地,只含有1个字母的串,可以看成是第一种模式的镜像串。 
    
        一个串可以含有许多镜像子串。我们的目标是求一个串的最大镜像子串(最长的镜像子串),如果有多个最大镜像子串,对称中心靠左的优先选中。例如:“abcdeefghhgfeiieje444k444lmn”的最大镜像子串是:“efghhgfe”
    
        下面的静态方法实现了该功能,请仔细阅读并分析代码,填写空白处的代码,使得程序的逻辑合理,结果正确。
    
    // 求最大(长度最大)镜像对称子串
    public static String getMaxMirrorString(String s)
    {
        String max_s = "";  // 所求的最大对称子串
    
        for(int i=0; i<s.length(); i++)
        {
            // 第一种对称模式
            int step = 1;
            try{
                for(;;)
                {
                    if(s.charAt(i-step) != s.charAt(i+step)) break;
                    step++;
                }
            }catch(Exception e){}
            
            String s1 = s.substring(_____________________________);     // 填空1
            
            
            // 第二种对称模式
            step = 0;
            try{
                for(;;)
                {
                    if(_________________________________) break;    // 填空2
                    step++;
                }
            }catch(Exception e){}
            
            String s2 = s.substring(i-step+1,i+step+1);
            
            
            if(s1.length() > max_s.length()) max_s = s1;
            if(s2.length() > max_s.length()) max_s = s2;
        }
        
        return max_s;                
    }
    
    【注意】
        只填写缺少的部分,不要抄写已有的代码。
        所填写代码不超过1条语句(句中不会含有分号)
        所填代码长度不超过256个字符。
        答案写在“解答.txt”中,不要写在这里!
    
    
    i-step+1, i+step
    s.charAt(i-step) != s.charAt(i+step+1)

    3 机器人行走

    【编程题】(满分18分)
    
        某少年宫引进了一批机器人小车。可以接受预先输入的指令,按指令行动。小车的基本动作很简单,只有3种:左转(记为L),右转(记为R),向前走若干厘米(直接记数字)。
    
        例如,我们可以对小车输入如下的指令:
    
        15L10R5LRR10R20
    
        则,小车先直行15厘米,左转,再走10厘米,再右转,...
    
        不难看出,对于此指令串,小车又回到了出发地。
    
        你的任务是:编写程序,由用户输入指令,程序输出每条指令执行后小车位置与指令执行前小车位置的直线距离。
    
    【输入、输出格式要求】
    
        用户先输入一个整数n(n<100),表示接下来将有n条指令。
    
        接下来输入n条指令。每条指令只由L、R和数字组成(数字是0~100之间的整数)
    
        每条指令的长度不超过256个字符。
    
        程序则输出n行结果。
    
        每条结果表示小车执行相应的指令前后位置的直线距离。要求四舍五入到小数后2位。
    
        例如:用户输入:
    5
    L100R50R10
    3LLL5RR4L12
    LL
    100R
    5L5L5L5
    
        则程序输出:
    102.96
    9.06
    0.00
    100.00
    0.00
    
    
    【注意】
    
        请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
        
        请把所有类写在同一个文件中,调试好后,存入与【考生文件夹】下对应题号的“解答.txt”中即可。
        
        相关的工程文件不要拷入。
        
        请不要使用package语句。
        
        源程序中只能出现JDK1.5中允许的语法或调用。不能使用1.6或更高版本。 
     1 import java.util.Scanner;
     2 
     3 
     4 public class Main {
     5     public static String Left = "ULDR";
     6     public static String Right = "URDL";
     7     
     8     public double getResult(String A) {
     9         double r = 0, x = 0, y = 0;
    10         char way = 'U';
    11         for(int i = 0;i < A.length();i++) {
    12             int start = i;
    13             if(A.charAt(start) >= '0' && A.charAt(start) <= '9') {
    14                 while(start < A.length() && A.charAt(start) >= '0' && A.charAt(start) <= '9')
    15                     start++;
    16                 int num = Integer.valueOf(A.substring(i, start));
    17                 if(way == 'U')
    18                     y += num;
    19                 else if(way == 'L')
    20                     x -= num;
    21                 else if(way == 'D')
    22                     y -= num;
    23                 else if(way == 'R')
    24                     x += num;
    25                 i = start - 1;
    26             } else {
    27                 char temp = A.charAt(i);
    28                 if(temp == 'L') {
    29                     int p = Left.indexOf(way+"");
    30                     p = (p + 1) % 4;
    31                     way = Left.charAt(p);
    32                 } else if(temp == 'R') {
    33                     int p = Right.indexOf(way+"");
    34                     p = (p + 1) % 4;
    35                     way = Right.charAt(p);
    36                 }
    37             }
    38         }
    39         r = Math.sqrt(x*x + y*y);
    40         return r;
    41     }
    42     
    43     public static void main(String[] args) {
    44         Main test = new Main();
    45         Scanner in = new Scanner(System.in);
    46         int n = in.nextInt();
    47         double[] result = new double[n];
    48         for(int i = 0;i < n;i++) {
    49             String A = in.next();
    50             result[i] = test.getResult(A);
    51         }
    52         for(int i = 0;i < n;i++) {
    53             System.out.printf("%.2f", result[i]);
    54             System.out.println();
    55         }
    56     }
    57 }

    4 地址格式转换

    【编程题】(满分21分)
    
        Excel是最常用的办公软件。每个单元格都有唯一的地址表示。比如:第12行第4列表示为:“D12”,第5行第255列表示为“IU5”。
        
        事实上,Excel提供了两种地址表示方法,还有一种表示法叫做RC格式地址。 第12行第4列表示为:“R12C4”,第5行第255列表示为“R5C255”。
    
        你的任务是:编写程序,实现从RC地址格式到常规地址格式的转换。
    
    【输入、输出格式要求】
    
        用户先输入一个整数n(n<100),表示接下来有n行输入数据。
    
        接着输入的n行数据是RC格式的Excel单元格地址表示法。
    
        程序则输出n行数据,每行是转换后的常规地址表示法。
    
        例如:用户输入:
    2
    R12C4
    R5C255
    
        则程序应该输出:
    D12
    IU5
    
    
    【注意】
    
        请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
        
        请把所有类写在同一个文件中,调试好后,存入与【考生文件夹】下对应题号的“解答.txt”中即可。
        
        相关的工程文件不要拷入。
        
        请不要使用package语句。
        
        源程序中只能出现JDK1.5中允许的语法或调用。不能使用1.6或更高版本。 
     1 import java.util.ArrayList;
     2 import java.util.Scanner;
     3 
     4 
     5 public class Main {
     6     public static String Position = "0ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     7     
     8     public String getResult(String A) {
     9         String r = "";
    10         ArrayList<Integer> list = new ArrayList<Integer>();
    11         int a = 0, b = 0;
    12         int m = A.indexOf('C');
    13         a = Integer.valueOf(A.substring(1, m));
    14         b = Integer.valueOf(A.substring(m + 1));
    15         while(b > 0) {
    16             list.add(b % 26);
    17             b = b / 26;
    18         }
    19         for(int i = list.size() - 1;i >= 0;i--) 
    20             r = r + Position.charAt(list.get(i));
    21         r = r + a;
    22         return r;
    23     }
    24     
    25     public static void main(String[] args) throws Exception {
    26         Main test = new Main();
    27         Scanner in = new Scanner(System.in);
    28         int n = in.nextInt();
    29         String[] result = new String[n];
    30         for(int i = 0;i < n;i++) {
    31             String A = in.next();
    32             result[i] = test.getResult(A);
    33         }
    34         for(int i = 0;i < n;i++)
    35             System.out.println(result[i]);
    36     }
    37 }

    5 排日程

    【编程题】(满分34分)
    
        某保密单位机要人员 A,B,C,D,E 每周需要工作5天,休息2天。
    
        上级要求每个人每周的工作日和休息日安排必须是固定的,不能在周间变更。
    
        此外,由于工作需要,还有如下要求:
    
        1. 所有人的连续工作日不能多于3天(注意:周日连到下周一也是连续)。
    
        2. 一周中,至少有3天所有人都是上班的。
    
        3. 任何一天,必须保证 A B C D 中至少有2人上班。
    
        4. B D E 在周日那天必须休息。
    
        5. A E 周三必须上班。
    
        6. A C 一周中必须至少有4天能见面(即同时上班)。
    
        你的任务是:编写程序,列出ABCDE所有可能的一周排班情况。工作日记为1,休息日记为0
        
        A B C D E 每人占用1行记录,从星期一开始。
    
    【输入、输出格式要求】
    
        程序没有输入,要求输出所有可能的方案。
    
        每个方案是7x5的矩阵。只有1和0组成。        
        
        矩阵中的列表示星期几,从星期一开始。
    
        矩阵的行分别表示A,B,C,D,E的作息时间表。
    
        多个矩阵间用空行分隔开。
    
        例如,如下的矩阵就是一个合格的解。请编程输出所有解(多个解的前后顺序不重要)。
    
    0110111
    1101110
    0110111
    1101110
    1110110
    
    【注意】
    
        请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
        
        请把所有类写在同一个文件中,调试好后,存入与【考生文件夹】下对应题号的“解答.txt”中即可。
        
        相关的工程文件不要拷入。
        
        请不要使用package语句。
        
        源程序中只能出现JDK1.5中允许的语法或调用。不能使用1.6或更高版本。 
      1 import java.util.ArrayList;
      2 
      3 
      4 public class Main {
      5     public static int[] S = {0, 1};
      6     public static ArrayList<String> list = new ArrayList<String>();
      7     public static ArrayList<String> result = new ArrayList<String>();
      8     
      9     public boolean check(int[] A) {
     10         int count = 0;
     11         for(int i = 0;i < 7;i++) {
     12             if(A[i] == 1)
     13                 count++;
     14             if(A[i % 7] == 1 && A[(i+1) % 7] == 1 && A[(i+2) % 7] == 1
     15                     && A[(i+3) % 7] == 1)
     16                 return false;
     17         }
     18         if(count == 5)
     19             return true;
     20         return false;
     21     }
     22     
     23     public void dfs(int[] A, int step) {
     24         if(step == A.length) {
     25             if(check(A)) {
     26                 StringBuilder s = new StringBuilder("");
     27                 for(int i = 0;i < A.length;i++)
     28                     s.append(A[i]);
     29                 if(!list.contains(s))
     30                     list.add(s.toString());
     31             }
     32             return;
     33         } else {
     34             for(int i = 0;i < 2;i++) {
     35                 A[step] = S[i];
     36                 dfs(A, step + 1);
     37                 A[step] = -1;
     38             }
     39         }
     40     }
     41     
     42     public boolean check1(String[] arrayA, int step) {
     43         if(step >= 0) {
     44             if(arrayA[0].charAt(2) != '1')
     45                 return false;
     46         }
     47         if(step >= 1) {
     48             if(arrayA[1].charAt(6) != '0')
     49                 return false;
     50         }
     51         if(step >= 2) {
     52             int count = 0;
     53             for(int i = 0;i < arrayA[0].length();i++) {
     54                 if(arrayA[0].charAt(i) == arrayA[2].charAt(i) &&
     55                         arrayA[0].charAt(i) == '1')
     56                     count++;
     57             }
     58             if(count < 4)
     59                 return false;
     60         }
     61         if(step >= 3) {
     62             if(arrayA[3].charAt(6) != '0')
     63                 return false;
     64             for(int i = 0;i < arrayA[0].length();i++) {
     65                 int count = 0;
     66                 if(arrayA[0].charAt(i) == '1')
     67                     count++;
     68                 if(arrayA[1].charAt(i) == '1')
     69                     count++;
     70                 if(arrayA[2].charAt(i) == '1')
     71                     count++;
     72                 if(arrayA[3].charAt(i) == '1')
     73                     count++;
     74                 if(count < 2)
     75                     return false;
     76             }
     77         }
     78         if(step >= 4) {
     79             if(arrayA[4].charAt(6) != '0' || arrayA[4].charAt(2) != '1')
     80                 return false;
     81             int count = 0;
     82             for(int i = 0;i < arrayA[0].length();i++) {
     83                 if(arrayA[0].charAt(i) == '1' && arrayA[1].charAt(i) 
     84                         == '1' && arrayA[2].charAt(i) == '1' && 
     85                         arrayA[3].charAt(i) == '1' && arrayA[4].charAt(i) == '1')
     86                     count++;
     87             }
     88             if(count < 3)
     89                 return false;
     90         }
     91         return true;
     92     }
     93     
     94     public void dfsResult(String[] arrayA, int step) {
     95         if(step == 5) {
     96             String s = "";
     97             for(int i = 0;i < arrayA.length;i++) {
     98                 s = s + arrayA[i] + "
    ";
     99             }
    100             if(!result.contains(s))
    101                 result.add(s);
    102             return;
    103         } else {
    104             for(int i = 0;i < list.size();i++) {
    105                 arrayA[step] = list.get(i);
    106                 if(check1(arrayA, step)) {
    107                     dfsResult(arrayA, step + 1);
    108                     arrayA[step] = "";
    109                 } else {
    110                     continue;
    111                 }
    112             }
    113         }
    114     }
    115     
    116     public void getResult() {
    117         int[] A = {-1,-1,-1,-1,-1,-1,-1};
    118         dfs(A, 0);
    119         String[] arrayA = new String[5];
    120         dfsResult(arrayA, 0);
    121         
    122         for(int i = 0;i < result.size();i++)
    123             System.out.println(result.get(i));
    124     }
    125     
    126     public static void main(String[] args) {
    127         Main test = new Main();
    128         test.getResult();
    129         
    130     }
    131 }
  • 相关阅读:
    常见网络攻击手段原理分析
    admins.py总结比较,转
    django的表与表之间的关系详细讲解
    django中的@login_required
    安装指定版本的第三方库
    在django中使用logging
    django的manytomany总结
    manyToManyField理解和用法
    django的多对一,一对一,多对多关系
    python 的os的总结
  • 原文地址:https://www.cnblogs.com/liuzhen1995/p/6847038.html
Copyright © 2020-2023  润新知