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

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

    题目1、 填算式

    【结果填空】 (满分11分)

    看这个算式:
    
    ☆☆☆ + ☆☆☆ = ☆☆☆
    
    如果每个五角星代表 1 ~ 9 的不同的数字。
    
    这个算式有多少种可能的正确填写方法?
    
    173 + 286 = 459
    295 + 173 = 468
    173 + 295 = 468
    183 + 492 = 675
    
    以上都是正确的填写法!
    
    注意:
    111 + 222 = 333 是错误的填写法!
    因为每个数字必须是不同的! 
    也就是说:1~9中的所有数字,每个必须出现且仅出现一次!
    
    注意:
    不包括数字“0”!
    
    注意:
    满足加法交换率的式子算两种不同的答案。
    所以答案肯定是个偶数!
    
    注意:
    只要求计算不同的填法的数目
    不要求列出所有填写法
    更不要求填写源代码!
    
    答案不要写在这里,请写在“解答.txt”中!
    

    336

    public class Main {
        public static int count = 0;
        
        public static void swap(int[] A, int i, int j) {
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
        }
        
        public static void dfs(int[] A, int step) {
            if(step == A.length) {
                int a = A[0]*100 + A[1]*10 + A[2];
                int b = A[3]*100 + A[4]*10 + A[5];
                int c = A[6]*100 + A[7]*10 + A[8];
                if(a + b == c)
                    count++;
                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) {
            int[] A = {1,2,3,4,5,6,7,8,9};
            dfs(A, 0);
            System.out.println(count);
        }
    }
    
    题目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或更高版本。

    import java.util.Scanner;
    
    
    public class Main {
        public static String Left = "ULDR";
        public static String Right = "URDL";
        
        public double getResult(String A) {
            double r = 0, x = 0, y = 0;
            char way = 'U';
            for(int i = 0;i < A.length();i++) {
                int start = i;
                if(A.charAt(start) >= '0' && A.charAt(start) <= '9') {
                    while(start < A.length() && A.charAt(start) >= '0' && A.charAt(start) <= '9')
                        start++;
                    int num = Integer.valueOf(A.substring(i, start));
                    if(way == 'U')
                        y += num;
                    else if(way == 'L')
                        x -= num;
                    else if(way == 'D')
                        y -= num;
                    else if(way == 'R')
                        x += num;
                    i = start - 1;
                } else {
                    char temp = A.charAt(i);
                    if(temp == 'L') {
                        int p = Left.indexOf(way+"");
                        p = (p + 1) % 4;
                        way = Left.charAt(p);
                    } else if(temp == 'R') {
                        int p = Right.indexOf(way+"");
                        p = (p + 1) % 4;
                        way = Right.charAt(p);
                    }
                }
            }
            r = Math.sqrt(x*x + y*y);
            return r;
        }
        
        public static void main(String[] args) {
            Main test = new Main();
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            double[] result = new double[n];
            for(int i = 0;i < n;i++) {
                String A = in.next();
                result[i] = test.getResult(A);
            }
            for(int i = 0;i < n;i++) {
                System.out.printf("%.2f", result[i]);
                System.out.println();
            }
        }
    }
    
    题目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或更高版本。

    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    public class Main {
        public static String Position = "0ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        
        public String getResult(String A) {
            String r = "";
            ArrayList<Integer> list = new ArrayList<Integer>();
            int a = 0, b = 0;
            int m = A.indexOf('C');
            a = Integer.valueOf(A.substring(1, m));
            b = Integer.valueOf(A.substring(m + 1));
            while(b > 0) {
                list.add(b % 26);
                b = b / 26;
            }
            for(int i = list.size() - 1;i >= 0;i--) 
                r = r + Position.charAt(list.get(i));
            r = r + a;
            return r;
        }
        
        public static void main(String[] args) throws Exception {
            Main test = new Main();
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            String[] result = new String[n];
            for(int i = 0;i < n;i++) {
                String A = in.next();
                result[i] = test.getResult(A);
            }
            for(int i = 0;i < n;i++)
                System.out.println(result[i]);
        }
    }
    
    题目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或更高版本。

    import java.util.ArrayList;
    
    
    public class Main {
        public static int[] S = {0, 1};
        public static ArrayList<String> list = new ArrayList<String>();
        public static ArrayList<String> result = new ArrayList<String>();
        
        public boolean check(int[] A) {
            int count = 0;
            for(int i = 0;i < 7;i++) {
                if(A[i] == 1)
                    count++;
                if(A[i % 7] == 1 && A[(i+1) % 7] == 1 && A[(i+2) % 7] == 1
                        && A[(i+3) % 7] == 1)
                    return false;
            }
            if(count == 5)
                return true;
            return false;
        }
        
        public void dfs(int[] A, int step) {
            if(step == A.length) {
                if(check(A)) {
                    StringBuilder s = new StringBuilder("");
                    for(int i = 0;i < A.length;i++)
                        s.append(A[i]);
                    if(!list.contains(s))
                        list.add(s.toString());
                }
                return;
            } else {
                for(int i = 0;i < 2;i++) {
                    A[step] = S[i];
                    dfs(A, step + 1);
                    A[step] = -1;
                }
            }
        }
        
        public boolean check1(String[] arrayA, int step) {
            if(step >= 0) {
                if(arrayA[0].charAt(2) != '1')
                    return false;
            }
            if(step >= 1) {
                if(arrayA[1].charAt(6) != '0')
                    return false;
            }
            if(step >= 2) {
                int count = 0;
                for(int i = 0;i < arrayA[0].length();i++) {
                    if(arrayA[0].charAt(i) == arrayA[2].charAt(i) &&
                            arrayA[0].charAt(i) == '1')
                        count++;
                }
                if(count < 4)
                    return false;
            }
            if(step >= 3) {
                if(arrayA[3].charAt(6) != '0')
                    return false;
                for(int i = 0;i < arrayA[0].length();i++) {
                    int count = 0;
                    if(arrayA[0].charAt(i) == '1')
                        count++;
                    if(arrayA[1].charAt(i) == '1')
                        count++;
                    if(arrayA[2].charAt(i) == '1')
                        count++;
                    if(arrayA[3].charAt(i) == '1')
                        count++;
                    if(count < 2)
                        return false;
                }
            }
            if(step >= 4) {
                if(arrayA[4].charAt(6) != '0' || arrayA[4].charAt(2) != '1')
                    return false;
                int count = 0;
                for(int i = 0;i < arrayA[0].length();i++) {
                    if(arrayA[0].charAt(i) == '1' && arrayA[1].charAt(i) 
                            == '1' && arrayA[2].charAt(i) == '1' && 
                            arrayA[3].charAt(i) == '1' && arrayA[4].charAt(i) == '1')
                        count++;
                }
                if(count < 3)
                    return false;
            }
            return true;
        }
        
        public void dfsResult(String[] arrayA, int step) {
            if(step == 5) {
                String s = "";
                for(int i = 0;i < arrayA.length;i++) {
                    s = s + arrayA[i] + "
    ";
                }
                if(!result.contains(s))
                    result.add(s);
                return;
            } else {
                for(int i = 0;i < list.size();i++) {
                    arrayA[step] = list.get(i);
                    if(check1(arrayA, step)) {
                        dfsResult(arrayA, step + 1);
                        arrayA[step] = "";
                    } else {
                        continue;
                    }
                }
            }
        }
        
        public void getResult() {
            int[] A = {-1,-1,-1,-1,-1,-1,-1};
            dfs(A, 0);
            String[] arrayA = new String[5];
            dfsResult(arrayA, 0);
            
            for(int i = 0;i < result.size();i++)
                System.out.println(result.get(i));
        }
        
        public static void main(String[] args) {
            Main test = new Main();
            test.getResult();
            
        }
    }
    
  • 相关阅读:
    Redis数据模型
    Redis集群使用的一些命令(持续更新)
    Redis简单集群搭建
    观察者模式
    抽象工厂模式
    简单工厂模式及其简单Java案例代码实现
    工厂方法模式及简单Java案例代码实现
    Java中的双重检查锁(double checked locking)
    BayaiM__MYSQL千万级数据量的优化方法积累__初级菜鸟
    BayaiM__Linux安装MySQL的两种方法
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13078155.html
Copyright © 2011-2022 走看看