第五届蓝桥杯省赛javaB组试题解析

    1 武功秘籍
        小明到X山洞探险,捡到一本有破损的武功秘籍(2000多页!当然是伪造的)。他注意到:书的第10页和第11页在同一张纸上,但第11页和第12页不在同一张纸上。

        小明只想练习该书的第81页到第92页的武功,又不想带着整本书。请问他至少要撕下多少张纸带走?

    这是个整数,请通过浏览器提交该数字,不要填写任何多余的内容。

7

    2、切面条

        一根高筋拉面,中间切一刀,可以得到2根面条。

        如果先对折1次,中间切一刀,可以得到3根面条。

        如果连续对折2次,中间切一刀,可以得到5根面条。

        那么,连续对折10次,中间切一刀,会得到多少面条呢?

    答案是个整数,请通过浏览器提交答案。不要填写任何多余的内容。

2^10 + 1 = 1025

3 猜字母

    把abcd...s共19个字母组成的序列重复拼接106次,得到长度为2014的串。

    接下来删除第1个字母(即开头的字母a),以及第3个,第5个等所有奇数位置的字母。

    得到的新串再进行删除奇数位置字母的动作。如此下去,最后只剩下一个字母,请写出该字母。

答案是一个小写字母,请通过浏览器提交答案。不要填写任何多余的内容。

q
package com.sihai.wujie;

public class _03 {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("abcdefghigklmnopqrs");
        for(int i = 1; i < 106; i++){
            sb.append("abcdefghigklmnopqrs");
        }

        String temp = sb.toString();
        System.out.println(temp.charAt(1));

        while(temp.length() != 1){
            String val = "";
            for(int i = 0; i < temp.length(); i++){
                if((i+1)%2 == 0){
                    val += temp.charAt(i);
                }
            }
            temp = val;
        }
        System.out.println(temp);

    }
}
4 大衍数列

    中国古代文献中,曾记载过“大衍数列”, 主要用于解释中国传统文化中的太极衍生原理。

    它的前几项是:0、2、4、8、12、18、24、32、40、50 ...

    其规律是:对偶数项,是序号平方再除2,奇数项,是序号平方减1再除2。

    以下的代码打印出了大衍数列的前 100 项。

for(int i=1; i<100; i++)
{
    if(________________)  //填空
        System.out.println(i*i/2);
    else
        System.out.println((i*i-1)/2);
}

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

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

i % 2 == 0

5 圆周率

    数学发展历史上,圆周率的计算曾有许多有趣甚至是传奇的故事。其中许多方法都涉及无穷级数。

    图1.png中所示,就是一种用连分数的形式表示的圆周率求法。

    下面的程序实现了该求解方法。实际上数列的收敛对x的初始值 并不敏感。    

    结果打印出圆周率近似值(保留小数点后4位,并不一定与圆周率真值吻合)。

    double x = 111; 
    for(int n = 10000; n>=0; n--){
        int i = 2 * n + 1;
        x = 2 + (i*i / x);
    }

    System.out.println(String.format("%.4f", ______________));

4 / (x - 1)

6 奇怪的分式

    上小学的时候,小明经常自己发明新算法。一次,老师出的题目是:

    1/4 乘以 8/5 

    小明居然把分子拼接在一起,分母拼接在一起,答案是:18/45 (参见图1.png)

    老师刚想批评他,转念一想,这个答案凑巧也对啊,真是见鬼!

    对于分子、分母都是 1~9 中的一位数的情况,还有哪些算式可以这样计算呢?

    请写出所有不同算式的个数(包括题中举例的)。

    显然,交换分子分母后,例如:4/1 乘以 5/8 是满足要求的,这算做不同的算式。

    但对于分子分母相同的情况,2/2 乘以 3/3 这样的类型太多了,不在计数之列!

注意:答案是个整数(考虑对称性,肯定是偶数)。请通过浏览器提交。不要书写多余的内容。

14

package com.sihai.wujie;

public class _06 {
    public static void main(String[] args) {
        int count = 0;
        for(int x = 1; x <= 9; x++){
            for(int y = 1; y <= 9; y++){
                if(x != y){
                for(int m = 1; m <= 9; m++){
                    for(int n = 1; n <= 9; n++){
                        if(m != n){
//                          double sum1 = (double)(n/m)*(y/x);
//                          double sum2 = (double)(n*10 + y)/(m*10 + x);
                            double sum1 = (double) y / x * n / m;
                            double sum2 = (double) (y * 10 + n)
                                        / (x * 10 + m);
                            if(sum1 == sum2){
                                count++;
                            }

                        }
                    }
                }
                }
            }
        }
        System.out.println(count);

    }


    /*public static void main(String[] args) {
        int count = 0;
        for (int a = 1; a <= 9; a++) {
            for (int b = 1; b <= 9; b++) {
                if (a != b) {
                    for (int c = 1; c <= 9; c++) {
                        for (int d = 1; d <= 9; d++) {
                            if (c != d) {
                                double sum1 = (double) b / a * d / c;
                                double sum2 = (double) (b * 10 + d)
                                        / (a * 10 + c);
                                if (sum1 == sum2) {
                                    count++;
                                }
                            }
                        }
                    }
                }
            }
            System.out.println(count);// 输出结果:14
        }
    }
        */
}
7 扑克序列
    A A 2 2 3 3 4 4, 一共4对扑克牌。请你把它们排成一行。
    要求:两个A中间有1张牌,两个2之间有2张牌,两个3之间有3张牌,两个4之间有4张牌。

    请填写出所有符合要求的排列中,字典序最小的那个。

例如:22AA3344 比 A2A23344 字典序小。当然,它们都不是满足要求的答案。


请通过浏览器提交答案。“A”一定不要用小写字母a,也不要用“1”代替。字符间一定不要留空格。

2342A3A4

public class Main {
    public static String result = "AAAAAAAA";

    public void swap(char[] A, int a, int b) {
        char temp = A[a];
        A[a] = A[b];
        A[b] = temp;
    }

    public void dfs(char[] arrayA, int step) {
        if(step == arrayA.length) {
            getResult(arrayA);
            return;
        } else {
            for(int i = step;i < arrayA.length;i++) {
                swap(arrayA, i, step);
                dfs(arrayA, step + 1);
                swap(arrayA, i, step);
            }
        }
        return;
    }

    public int getSub(char[] arrayA, char temp) {
        int a1 = 0, a2 = 0;
        int i = 0;
        for(;i < arrayA.length;i++) {
            if(arrayA[i] == temp) {
                a1 = i;
                break;
            }
        }
        i++;
        for(;i < arrayA.length;i++) {
            if(arrayA[i] == temp) {
                a2 = i;
                break;
            }
        }
        return (a2 - a1);
    }

    public void getResult(char[] arrayA) {
        char temp1 = 'A', temp2 = '2', temp3 = '3', temp4 = '4';
        boolean judge = false;
        int a = getSub(arrayA, temp1);
        int b = getSub(arrayA, temp2);
        int c = getSub(arrayA, temp3);
        int d = getSub(arrayA, temp4);
        if(a == 2 && b == 3 && c == 4 && d == 5)
            judge = true;
        if(judge == false)
            return;
        String A = "";
        for(int i = 0;i < arrayA.length;i++)
            A += arrayA[i];
        if(result.compareTo(A) > 0)
            result = A;
        return;
    }

    public static void main(String[] args) {
        Main test = new Main();
        String A = "AA223344";
        char[] arrayA = A.toCharArray();
        test.dfs(arrayA, 0);
        System.out.println(result);
    }
}
8 分糖果

    有n个小朋友围坐成一圈。老师给每个小朋友随机发偶数个糖果,然后进行下面的游戏:

    每个小朋友都把自己的糖果分一半给左手边的孩子。

    一轮分糖后,拥有奇数颗糖的孩子由老师补给1个糖果,从而变成偶数。

    反复进行这个游戏,直到所有小朋友的糖果数都相同为止。

    你的任务是预测在已知的初始糖果情形下,老师一共需要补发多少个糖果。

【格式要求】

    程序首先读入一个整数N(2<N<100),表示小朋友的人数。
    接着是一行用空格分开的N个偶数(每个偶数不大于1000,不小于2)
    要求程序输出一个整数,表示老师需要补发的糖果数。

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

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


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

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

public class Main {
    public static long count = 0;

    public boolean judge(int[] A) {
        for(int i = 1;i < A.length;i++) {
            if(A[i - 1] == A[i])
                continue;
            else
                return false;
        }
        return true;
    }

    public void getResult(int[] A) {
        int[] tempA = new int[A.length];
        while(true) {
            for(int i = 0;i < A.length;i++)
                tempA[i] = A[i] / 2;
            A[0] = A[0] - tempA[0] + tempA[A.length - 1];
            for(int i = 1;i < A.length;i++)
                A[i] = A[i] - tempA[i] + tempA[i - 1];
            for(int i = 0;i < A.length;i++) {
                if(A[i] % 2 == 1) {
                    count++;
                    A[i] += 1;
                }
            }
            if(judge(A) == true)
                break;
        }
        System.out.println(count);
        return;
    }

    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] A = new int[n];
        for(int i = 0;i < n;i++)
            A[i] = in.nextInt();
        test.getResult(A);
    }
}
9 地宫取宝

    X 国王有一个地宫宝库。是 n x m 个格子的矩阵。每个格子放一件宝贝。每个宝贝贴着价值标签。

    地宫的入口在左上角,出口在右下角。

    小明被带到地宫的入口,国王要求他只能向右或向下行走。

    走过某个格子时,如果那个格子中的宝贝价值比小明手中任意宝贝价值都大,小明就可以拿起它(当然,也可以不拿)。

    当小明走到出口时,如果他手中的宝贝恰好是k件,则这些宝贝就可以送给小明。

    请你帮小明算一算,在给定的局面下,他有多少种不同的行动方案能获得这k件宝贝。

【数据格式】

    输入一行3个整数,用空格分开:n m k (1<=n,m<=50, 1<=k<=12)

    接下来有 n 行数据,每行有 m 个整数 Ci (0<=Ci<=12)代表这个格子上的宝物的价值

    要求输出一个整数,表示正好取k个宝贝的行动方案数。该数字可能很大,输出它对 1000000007 取模的结果。

例如,输入:
2 2 2
1 2
2 1
程序应该输出:
2

再例如,输入:
2 3 2
1 2 3
2 1 5
程序应该输出:
14

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


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

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

public class Main {
    public static long count = 0;
    public static int[][] move = {{0,1},{1,0}}; //表示分别向右、下移动一步

    static class point {
        public int x;
        public int y;
        public ArrayList<Integer> list;

        point(int x, int y, ArrayList<Integer> list) {
            this.x = x;
            this.y = y;
            this.list = list;
        }
    }

    //获取C(n , m)值
    public long getCnm(int n, int m) {
        if(m == 0 || n == m)
            return 1;
        long result = 0;
        long temp1 = 1, temp2 = 1;
        for(int i = 0;i < m;i++)
            temp1 *= (n - i);
        for(int i = 1;i <= m;i++)
            temp2 *= i;
        result = temp1 / temp2;
        return result;
    }

    public boolean check(int[][] A, int x, int y) {
        if(x > A.length - 1 || y > A[0].length - 1)
            return false;
        return true;
    }

    public void bfs(int[][] A, int x, int y, int k) {
        ArrayList<point> list = new ArrayList<point>();
        ArrayList<Integer> listV = new ArrayList<Integer>();
        listV.add(A[x][y]);
        list.add(new point(x, y, listV));
        while(list.size() > 0) {
            point temp = list.get(0);
            int tempX = temp.x;
            int tempY = temp.y;
            ArrayList<Integer> templistV = temp.list;
            list.remove(0);
            if(tempX == A.length - 1 && tempY == A[0].length - 1) {
                getResult(templistV, k);
                continue;
            }

            for(int i = 0;i < 2;i++) {
                int tempX1 = tempX + move[i][0];
                int tempY1 = tempY + move[i][1];
                if(check(A, tempX1, tempY1)) {
                    ArrayList<Integer> templistV1 = new ArrayList<Integer>();
                    for(int j = 0;j < templistV.size();j++)
                        templistV1.add(templistV.get(j));
                    templistV1.add(A[tempX1][tempY1]);
                    list.add(new point(tempX1, tempY1, templistV1));
                }
            }
        }
    }

    public void getResult(ArrayList<Integer> listV, int k) {
        int len = listV.size();
        for(int i = 0;i <= len - k;i++) {
            int n = 1;
            for(int j = i + 1;j < len;j++) {
                if(listV.get(j) > listV.get(i))
                    n++;
            }
            if(n >= k) {
                //此处使用n-1,k-1。是因为当第i个元素必须要选择
                count = (count + getCnm(n - 1, k - 1)) % 1000000007;
            }
        }
        return;
    }

    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();
        int[][] A = new int[n][m];
        for(int i = 0;i < n;i++)
            for(int j = 0;j < m;j++)
                A[i][j] = in.nextInt();
        test.bfs(A, 0, 0, k);
        System.out.println(count);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello-java-maker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值