算法笔记_111:第五届蓝桥杯软件类省赛真题(Java本科A组)试题解答

 目录

1 猜年龄

2 李白打酒

3 神奇算式

4 写日志

5 锦标赛

6 六角填数

7 绳圈

8 兰顿蚂蚁

9 斐波那契

10 波动数列

 

前言:以下试题解答代码部分仅供参考,若有不当之处,还请路过的同学提醒一下~


1 猜年龄

标题:猜年龄

    小明带两个妹妹参加元宵灯会。别人问她们多大了,她们调皮地说:“我们俩的年龄之积是年龄之和的6倍”。小明又补充说:“她们可不是双胞胎,年龄差肯定也不超过8岁啊。”

    请你写出:小明的较小的妹妹的年龄。


注意: 只写一个人的年龄数字,请通过浏览器提交答案。不要书写任何多余的内容。

10
public class Main {
    
    public void printResult() {
        for(int x = 1;x < 100;x++) {
            for(int y = 1;y < 100;y++) {
                int a = x * y;
                int b = 6 * (x + y);
                if(a == b && Math.abs(x - y) < 8 && x - y != 0) {
                    System.out.println("x = "+x+", y = "+y);
                }
            }
        }
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        test.printResult();
    }
}

2 李白打酒

标题:李白打酒

    话说大诗人李白,一生好饮。幸好他从不开车。

    一天,他提着酒壶,从家里出来,酒壶中有酒2斗。他边走边唱:

    无事街上走,提壶去打酒。
    逢店加一倍,遇花喝一斗。

    这一路上,他一共遇到店5次,遇到花10次,已知最后一次遇到的是花,他正好把酒喝光了。 

    请你计算李白遇到店和花的次序,可以把遇店记为a,遇花记为b。则:babaabbabbabbbb 就是合理的次序。像这样的答案一共有多少呢?请你计算出所有可能方案的个数(包含题目给出的)。

    注意:通过浏览器提交答案。答案是个整数。不要书写任何多余的内容。


14
public class Main {
    public static int count = 0;
    
    public void dfs(int sum, int step, int step1, int step2) {
        if(step > 15 || (step < 15 && sum == 0))//题意规定,共走15次,最后一次还剩1斗
            return;
        if(step == 15) {
            if(sum == 0 && step1 == 5 && step2 == 10)
                count++;
            return;
        }
        dfs(sum * 2, step + 1, step1 + 1, step2);
        dfs(sum - 1, step + 1, step1, step2 + 1);
        return;
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        test.dfs(2, 0, 0, 0);
        System.out.println(count);
    }
}

3 神奇算式

标题:神奇算式

    由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。

    比如: 

210 x 6 = 1260 
8 x 473 = 3784
27 x 81 = 2187 

    都符合要求。

    如果满足乘法交换律的算式算作同一种情况,那么,包含上边已列出的3种情况,一共有多少种满足要求的算式。

    请填写该数字,通过浏览器提交答案,不要填写多余内容(例如:列出所有算式)。

12
import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static int count1 = 0;
    public static int count2 = 0;
    
    public boolean judge(int a, int b) {
        int num = a * b;
        if(num < 1000)
            return false;
        ArrayList<Integer> list = new ArrayList<Integer>();
        while(a > 0) {
            list.add(a % 10);
            a = a / 10;
        }
        while(b > 0) {
            list.add(b % 10);
            b = b / 10;
        }
        Collections.sort(list);     //对list中元素进行从小到大排序
        for(int i = 1;i < list.size();i++) {
            if(list.get(i - 1) == list.get(i))
                return false;
        }
        ArrayList<Integer> list1 = new ArrayList<Integer>();
        while(num > 0) {
            list1.add(num % 10);
            num = num / 10;
        }
        Collections.sort(list1);
        if(list.size() == list1.size()) {
            int i = 0;
            for(;i < list.size();i++) {
                if(list.get(i) == list1.get(i))
                    continue;
                else
                    return false;
            }
            if(i == list.size())
                return true;
        }
        return false;
    }
    
    public void printResult() {
        for(int i = 1;i < 10;i++) {
            for(int j = 100;j < 1000;j++) {
                if(judge(i, j))
                    count1++;
            }
        }
        
        for(int i = 10;i < 100;i++) {
            for(int j = 10;j < 100;j++) {
                if(judge(i, j))
                    count2++;
            }
        }
        int result = count1 + count2 / 2;
        System.out.println("result = "+result);
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        test.printResult();
    }
}

4 写日志

标题:写日志

    写日志是程序的常见任务。现在要求在 t1.log, t2.log, t3.log 三个文件间轮流写入日志。也就是说第一次写入t1.log,第二次写入t2.log,... 第四次仍然写入t1.log,如此反复。

    下面的代码模拟了这种轮流写入不同日志文件的逻辑。

public class A
{
    private static int n = 1;
    
    public static void write(String msg)
    {
        String filename = "t" + n + ".log";
        n = ____________;
        System.out.println("write to file: " + filename + " " + msg);
    }
}

    请填写划线部分缺失的代码。通过浏览器提交答案。

注意:不要填写题面已有的内容,也不要填写任何说明、解释文字。

(n + 1) % 3 == 0 ? 3 : (n + 1) % 3

5 锦标赛

标题:锦标赛

   如果要在n个数据中挑选出第一大和第二大的数据(要求输出数据所在位置和值),使用什么方法比较的次数最少?我们可以从体育锦标赛中受到启发。

   如图【1.png】所示,8个选手的锦标赛,先两两捉对比拼,淘汰一半。优胜者再两两比拼...直到决出第一名。

   第一名输出后,只要对黄色标示的位置重新比赛即可。

   下面的代码实现了这个算法(假设数据中没有相同值)。

   代码中需要用一个数组来表示图中的树(注意,这是个满二叉树, 不足需要补齐)。它不是存储数据本身,而是存储了数据的下标。   
   
   第一个数据输出后,它所在的位置被标识为-1

class A{
       //a 表示待处理的数据,长度如果不是2的次幂,则不足位置补为-1
    static void pick(int[] a)
    {
        int n = 1;
        while(n<a.length) n *= 2;
        
        
        int[] b = new int[2*n-1];
        for(int i=0; i<n; i++){ 
            if(i<a.length) 
                b[n-1+i] = i;
            else
                b[n-1+i] = -1;
        }
        
        //从最后一个向前处理
        for(int i=b.length-1; i>0; i-=2){
            if(b[i]<0){
                if(b[i-1]>=0)
                    b[(i-1)/2] = b[i-1]; 
                else
                    b[(i-1)/2] = -1;
            }
            else{
                if(a[b[i]]>a[b[i-1]])
                    b[(i-1)/2] = b[i];
                else
                    b[(i-1)/2] = b[i-1];
            }
        }
        
        //输出树根
        System.out.println(b[0] + ": " + a[b[0]]);
        
        //值等于根元素的位置需要重新pk
        pk(a,b,0,b[0]);
        
        //再次输出树根
        System.out.println(b[0] + ": " + a[b[0]]);
    }

    // a 表示待处理数据,b 二叉树,k 当前要重新比拼的位置,v 已经决胜出的值    
       static void pk(int[] a, int[] b, int k, int v)
    {
        
        int k1 = k*2+1;
        int k2 = k1 + 1;
        
        if(k1>=b.length || k2>=b.length){
            b[k] = -1;
            return;
        }
        
        if(b[k1]==v) 
            pk(a,b,k1,v);
        else
            pk(a,b,k2,v);
        
        
        //重新比较
        if(b[k1]<0){
            if(b[k2]>=0)
                b[k] = b[k2]; 
            else
                b[k] = -1;
            return;
        }
        
        if(b[k2]<0){
            if(b[k1]>=0)
                b[k] = b[k1]; 
            else
                b[k] = -1;
            return;
        }
        
        if(__________________________)  //填空
            b[k] = b[k1];
        else
            b[k] = b[k2];
    }
}


    请仔细分析流程,填写缺失的代码。

    通过浏览器提交答案,只填写缺失的代码,不要填写已有代码或其它说明语句等。

a[b[k1]] > a[b[k2]]

6 六角填数

标题:六角填数

    如图【1.png】所示六角形中,填入1~12的数字。

    使得每条直线上的数字之和都相同。

    图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?

请通过浏览器提交答案,不要填写多余的内容。

10
public class Main {
    
    public boolean check(int[] A) {
        int[] B = new int[12];
        B[0] = 1;
        B[1] = 8;
        B[11] = 3;
        for(int i = 2;i <= 10;i++)
            B[i] = A[i - 2];
        int num = B[1] + B[2] + B[3] + B[4];
        if(B[0] + B[2] + B[5] + B[7] != num)
            return false;
        if(B[0] + B[3] + B[6] + B[10] != num)
            return false;
        if(B[7] + B[8] + B[9] + B[10] != num)
            return false;
        if(B[1] + B[5] + B[8] + B[11] != num)
            return false;
        if(B[4] + B[6] + B[9] + B[11] != num)
            return false;
        for(int i = 0;i < 12;i++)
            System.out.print(B[i]+" ");
        System.out.println();
        return true;
    }
    
    public void swap(int[] A, int a, int b) {
        int temp = A[a];
        A[a] = A[b];
        A[b] = temp;
    }
    
    public void dfs(int[] A, int step) {
        if(step == 9) {
            if(check(A))
                System.out.println(A[3]);
            return;
        } else {
            for(int i = step;i < 9;i++) {
                swap(A, i, step);
                dfs(A, step + 1);
                swap(A, i, step);
            }
        }
        return;
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        int[] A = {2,4,5,6,7,9,10,11,12};
        test.dfs(A, 0);
    }
}

7 绳圈

标题:绳圈

    今有 100 根绳子,当然会有 200 个绳头。

    如果任意取绳头两两配对,把所有绳头都打结连接起来。最后会形成若干个绳圈(不考虑是否套在一起)。

    我们的问题是:请计算最后将形成多少个绳圈的概率最大?

    注意:结果是一个整数,请通过浏览器提交该数字。不要填写多余的内容。

3


本题对数学的推理要求比较高,首先我们要分析,现在有n根绳子,那么任意选取其中两个绳头配对,直到所有绳头均已完成配对,有多少种情况呢?

假设n-1根绳子配对完毕共有f(n - 1)种情,那么在此基础上加一根绳子,重新进行配对,有以下两种情况可以选择:(1)绳圈个数不变,在n - 1根已配对完毕的绳头中选择一个绳头和当前新添加的一根绳子绳头打结连接起来;(2)增加一个绳圈,直接让新添加的绳子两个绳头直接相连。

所以f(n) = f(n - 1) * (C(1, 2*(n - 1))+ C(0, 2*(n - 1))) = f(n - 1)  * (2*n - 2 + 1) = f(n - 1) * (2*n - 1)

有了上述公式,那么可以推导出解决本题的动态转移方程,dp[i][j] = dp[i - 1][j] * (2*n - 2) / (2*n - 1) + dp[i - 1][j - 1] * (1) / (2*n - 1)

dp[i][j]表示当前有i根绳子,形成j个绳圈的概率。(PS:j > i,其概率为0)
public class Main {
    
    public static void main(String[] args) {
        double[][] dp = new double[101][101];
        dp[1][1] = 1;  //当前只有一根绳子,只能形成一个绳圈,且概率为1
        for(int i = 2;i < 101;i++) {  //绳子数
            for(int j = 1;j < 101;j++) {  //绳圈数
                if(j > i)  //此时的情形不可能出现,即此时概率为0
                    continue;
                dp[i][j] = dp[i - 1][j]*(2*i - 2) / (2*i - 1) + dp[i][j -1]/(2*i - 1);
            }
        }
        double max = 0;
        int maxI = 0;
        for(int i  = 1;i < 101;i++) {
            if(max < dp[100][i]) {
                max = dp[100][i];
                maxI = i;
            }
        }
        System.out.println(maxI);
    }
}

8 兰顿蚂蚁

标题:兰顿蚂蚁

    兰顿蚂蚁,是于1986年,由克里斯·兰顿提出来的,属于细胞自动机的一种。

    平面上的正方形格子被填上黑色或白色。在其中一格正方形内有一只“蚂蚁”。
    蚂蚁的头部朝向为:上下左右其中一方。

    蚂蚁的移动规则十分简单:
    若蚂蚁在黑格,右转90度,将该格改为白格,并向前移一格;
    若蚂蚁在白格,左转90度,将该格改为黑格,并向前移一格。

    规则虽然简单,蚂蚁的行为却十分复杂。刚刚开始时留下的路线都会有接近对称,像是会重复,但不论起始状态如何,蚂蚁经过漫长的混乱活动后,会开辟出一条规则的“高速公路”。

    蚂蚁的路线是很难事先预测的。

    你的任务是根据初始状态,用计算机模拟兰顿蚂蚁在第n步行走后所处的位置。

【数据格式】

输入数据的第一行是 m n 两个整数(3 < m, n < 100),表示正方形格子的行数和列数。
接下来是 m 行数据。
每行数据为 n 个被空格分开的数字。0 表示白格,1 表示黑格。

接下来是一行数据:x y s k, 其中x y为整数,表示蚂蚁所在行号和列号(行号从上到下增长,列号从左到右增长,都是从0开始编号)。s 是一个大写字母,表示蚂蚁头的朝向,我们约定:上下左右分别用:UDLR表示。k 表示蚂蚁走的步数。

输出数据为两个空格分开的整数 p q, 分别表示蚂蚁在k步后,所处格子的行号和列号。


例如, 输入:
5 6
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
2 3 L 5
程序应该输出:
1 3

再例如, 输入:
3 3
0 0 0
1 1 1
1 1 1
1 1 U 6
程序应该输出:
0 0


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


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

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

public class Main {
    public static int[][] move = {{-1,0},{1,0},{0,-1},{0,1}};//表示分别向上、下、左、右移动一步
    public static int[] left = {2,1,3,0}; //向左转90度,其中2向左、1向下、3向右、0向上
    public static int[] right = {2,0,3,1}; //向右转90度,具体数字含义同上
    
    public void bfs(int[][] value, int x, int y, String tempArea, int count) {
        int area = 0;
        if(tempArea.equals("L"))
            area = 2;
        else if(tempArea.equals("R"))
            area = 3;
        else if(tempArea.equals("U"))
            area = 0;
        else if(tempArea.equals("D"))
            area = 1;
        int step = 0;
        while(step < count) {
            if(value[x][y] == 1) {  //黑色格子,当前方向area要向右转90度
                value[x][y] = 0;
                int i = 0;
                for(;i < 4;i++) {
                    if(right[i] == area)
                        break;
                }
                i = (i + 1) % 4;
                area = right[i];
                x = x + move[area][0];
                y = y + move[area][1];        
                step++;
            } else {   //白色格子,当前方向area要向左转90度
                value[x][y] = 1;
                int i = 0;
                for(;i < 4;i++) {
                    if(left[i] == area)
                        break;
                }
                i = (i + 1) % 4;
                area = left[i];
                x = x + move[area][0];
                y = y + move[area][1];
                step++;
            }
        }
        System.out.println(x+" "+y);
        return;
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        int[][] value = new int[m][n];
        for(int i = 0;i < m;i++)
            for(int j = 0;j < n;j++)
                value[i][j] = in.nextInt();
        int x = in.nextInt();
        int y = in.nextInt();
        String tempArea = in.next();
        int count  = in.nextInt();
        test.bfs(value, x, y, tempArea, count);
    }
}

9 斐波那契

标题:斐波那契

    斐波那契数列大家都非常熟悉。它的定义是:

    f(x) = 1                    .... (x=1,2)
    f(x) = f(x-1) + f(x-2)      .... (x>2)

    对于给定的整数 n 和 m,我们希望求出:
    f(1) + f(2) + ... + f(n)  的值。但这个值可能非常大,所以我们把它对 f(m) 取模。
    公式参见【图1.png】

    但这个数字依然很大,所以需要再对 p 求模。

【数据格式】
输入为一行用空格分开的整数 n m p (0 < n, m, p < 10^18)
输出为1个整数

例如,如果输入:
2 3 5
程序应该输出:
0

再例如,输入:
15 11 29
程序应该输出:
25

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


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

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


首先,关于斐波那契数的求取,如果使用递归法求取,会出现远远超时;迭代法求取差不多也会超时,此处,最好使用矩阵相乘法求取第n个斐波那契数。

其次,关于求取前n个斐波那契数和的问题,利用斐波那契数的性质(网上资料参考所得):S(n) = F(n+2) - 1,其中S(n)是前n个斐波那契数的和,F(n + 2)是第n+2个斐波那契数。

最后,要考虑n,m的取值问题,经过使用计算机运算检测,一般n > 100,F(n)就会超过long型最大值,所以此处建议使用BigInteger类型,来存储斐波那契数。

下面的代码仅供参考,不保证n、m、p达到10^18数量级时,大整数类型的取余不会出现内存溢出问题哦。
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static BigInteger[][] ONE = {{BigInteger.ONE, BigInteger.ONE},
        {BigInteger.ONE,BigInteger.ZERO}};
    public static BigInteger[][] ZERO = {{BigInteger.ZERO,BigInteger.ZERO},
        {BigInteger.ZERO,BigInteger.ZERO}};
    //求取矩阵ONE的n次方
    public BigInteger[][] getOneOfN(long n) {
        if(n == 0)
            return ZERO;
        if(n == 1)
            return ONE;
        if((n & 1) == 0) {   //当n为偶数时
            BigInteger[][] A = getOneOfN(n >> 1);
            return multiMatrix(A, A);
        }
        //当n为奇数时
        BigInteger[][] A = getOneOfN(n >> 1);
        return multiMatrix(multiMatrix(A, A), ONE);
    }
    //求取矩阵A*B的值
    public BigInteger[][] multiMatrix(BigInteger[][] A, BigInteger[][] B) {
        BigInteger[][] result = new BigInteger[A.length][B[0].length];
        for(int i = 0;i < A.length;i++)
            for(int j = 0;j < B[0].length;j++)
                result[i][j] = BigInteger.ZERO;
        for(int i = 0;i < A.length;i++)
            for(int j = 0;j < B.length;j++)
                for(int k = 0;k < A[0].length;k++)
                    result[i][j] = result[i][j].add(A[i][k].multiply(B[k][j]));
        return result;
    }
    //获取第n个斐波那契数
    public BigInteger getFibonacci(long n) {
        if(n == 1 || n == 2)
            return BigInteger.ONE;
        BigInteger[][] A  = new BigInteger[1][2];
        A[0][0] = BigInteger.ONE;
        A[0][1] = BigInteger.ONE;
        BigInteger[][] B = getOneOfN(n - 2);
        A = multiMatrix(A, B);
        return A[0][0];
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();
        long m = in.nextLong();
        BigInteger p = in.nextBigInteger();
        BigInteger result = BigInteger.ZERO;
        result = test.getFibonacci(n + 2).subtract(BigInteger.ONE);
        result = result.mod(test.getFibonacci(m));
        result = result.mod(p);
        System.out.println(result);
    }
    
}

10 波动数列

标题:波动数列

    观察这个数列:
    1 3 0 2 -1 1 -2 ...

    这个数列中后一项总是比前一项增加2或者减少3。

    栋栋对这种数列很好奇,他想知道长度为 n 和为 s 而且后一项总是比前一项增加a或者减少b的整数数列可能有多少种呢?

【数据格式】
    输入的第一行包含四个整数 n s a b,含义如前面说述。
    输出一行,包含一个整数,表示满足条件的方案数。由于这个数很大,请输出方案数除以100000007的余数。

例如,输入:
4 10 2 3
程序应该输出:
2

【样例说明】
这两个数列分别是2 4 1 3和7 4 1 -2。

【数据规模与约定】
对于10%的数据,1<=n<=5,0<=s<=5,1<=a,b<=5;
对于30%的数据,1<=n<=30,0<=s<=30,1<=a,b<=30;
对于50%的数据,1<=n<=50,0<=s<=50,1<=a,b<=50;
对于70%的数据,1<=n<=100,0<=s<=500,1<=a, b<=50;
对于100%的数据,1<=n<=1000,-1,000,000,000<=s<=1,000,000,000,1<=a, b<=1,000,000。

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


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

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

public class Main {
    public static int count = 0;
    public static int n = 0;
    public static int s = 0;
    public static int a = 0;
    public static int b = 0;
    public static int sum = 0;
    
    public void dfs(int i, int step) {
        if(step == n) {
            if(sum == s)
                count = (count + 1) % 100000007;
            return;
        }
        sum += i;
        dfs(i + a, step + 1);
        dfs(i - b, step + 1);
        sum -= i;
        return;
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        s = in.nextInt();
        a = in.nextInt();
        b = in.nextInt();
        for(int i=s-n*b;i<s+n*a;i++)
            test.dfs(i, 1);
        System.out.println(count);
    }
}
原文地址:https://www.cnblogs.com/liuzhen1995/p/6619041.html