zoukankan      html  css  js  c++  java
  • 算法笔记_112:第五届蓝桥杯软件类省赛真题(Java本科B组)试题解答

     目录

    1 武功秘籍

    2 切面条

    3 猜字母

    4 大衍数列

    5 圆周率

    6 奇怪的分式

    7 扑克序列

    8 分糖果

    9 地宫取宝

    10 矩阵翻硬币

     

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


    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
    import java.util.ArrayList;
    
    public class Main {
        
        public static void main(String[] args) {
            ArrayList<Character> list = new ArrayList<Character>();
            list.add('0');      //第0位元素
            for(int i = 1;i <= 106;i++) {
                for(int j = 0;j < 19;j++) {
                    char temp = 'a';
                    temp = (char) (temp + j);
                    list.add(temp);
                }
            }
            for(int i = 1;i <=19;i++)
                System.out.print(list.get(i)+" ");
            System.out.println();
            while(list.size() > 2) {
                ArrayList<Character> tempList = new ArrayList<Character>();
                tempList.add('0');
                for(int i = 2;i < list.size();i = i + 2) {
                    tempList.add(list.get(i));
                }
                list = tempList;
            }
            System.out.println(list.size()+", "+list.get(0)+", "+list.get(1));
        }
    }

    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
    public class Main {
        public static int count = 0;
        
        public void printResult() {
            for(int a = 1;a < 10;a++) {
                for(int b = 1;b < 10;b++) {
                    if(b == a)
                        continue;
                    for(int c = 1;c < 10;c++) {
                        for(int d = 1;d < 10;d++) {
                            if(d == c)
                                continue;
                            int temp1 = b * d * (a * 10 + c);
                            int temp2 = (b * 10 + d) * a * c;
                            if(temp1 == temp2)
                                count++;
                        }
                    }
                }
            }
            System.out.println(count);
        }
        
        public static void main(String[] args) {
            Main test = new Main();
            test.printResult();
        }
    }

    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 矩阵翻硬币

    标题:矩阵翻硬币
    
        小明先把硬币摆成了一个 n 行 m 列的矩阵。
    
        随后,小明对每一个硬币分别进行一次 Q 操作。
    
        对第x行第y列的硬币进行 Q 操作的定义:将所有第 i*x 行,第 j*y 列的硬币进行翻转。
    
        其中i和j为任意使操作可行的正整数,行号和列号都是从1开始。
    
        当小明对所有硬币都进行了一次 Q 操作后,他发现了一个奇迹——所有硬币均为正面朝上。
    
        小明想知道最开始有多少枚硬币是反面朝上的。于是,他向他的好朋友小M寻求帮助。
    
        聪明的小M告诉小明,只需要对所有硬币再进行一次Q操作,即可恢复到最开始的状态。然而小明很懒,不愿意照做。于是小明希望你给出他更好的方法。帮他计算出答案。
    
    【数据格式】
        输入数据包含一行,两个正整数 n m,含义见题目描述。
        输出一个正整数,表示最开始有多少枚硬币是反面朝上的。
    
    【样例输入】
    2 3
    
    【样例输出】
    1
    
    【数据规模】
    对于10%的数据,n、m <= 10^3;
    对于20%的数据,n、m <= 10^7;
    对于40%的数据,n、m <= 10^15;
    对于10%的数据,n、m <= 10^1000(10的1000次方)。
    
    资源约定:
    峰值内存消耗(含虚拟机) < 256M
    CPU消耗  < 2000ms
    
    
    请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
    
    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
    注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
    注意:主类的名字必须是:Main,否则按无效代码处理。
    
    
    根据网上资料参考,要解决本题大数据输入问题,使用大整数类型,且输出结果满足以下公式:f(x) =(n^1/2) * (m^1/2),两者的开平方四舍五入的整数相乘。
    
    
    下面代码,使用暴力求解,对于小于int类型最大值的数据符合,代码仅供参考。
    import java.util.Scanner;
    
    public class Main {
        public static long n = 0;
        public static long m = 0;
        
        public boolean check(long x, long y) {
            if(x < 1 || x > n || y < 1 || y > m)
                return false;
            return true;
        }
        
        public void getResult(long[][] value) {
            for(long i = 1;i <= n;i++) {
                for(long j = 1;j <= m;j++) {
                    for(long x = 1;x <= n/i;x++) {
                        for(long y = 1;y <= m/j;y++) {
                            long tempX = x * i;
                            long tempY = y * j;
                            if(check(tempX, tempY)) {
                                value[(int) tempX][(int) tempY] = (value[(int) tempX][(int) tempY] + 1) % 2;
                            }
                        }
                    }
                }
            }
            long count = 0;
            for(long i = 1;i <= n;i++) {
                for(long j = 1;j <= m;j++) {
                    if(value[(int) i][(int) j] == 1)
                        count++;
                }
            }
            System.out.println(count);
            return;
        }
        
        public static void main(String[] args) {
            Main test = new Main();
            Scanner in = new Scanner(System.in);
            n = in.nextLong();
            m = in.nextLong();
            long[][] value = new long[(int) (n + 1)][(int) (m + 1)];
            test.getResult(value);
        }
    }
  • 相关阅读:
    Debian Linux下如何以root账号登录桌面
    原 Debian设置开机自动启动与关闭
    Qt中使用QProcess备份和恢复Mysql数据库
    mysqldump 的一些使用参数
    Mysql导出表结构及表数据 mysqldump用法
    启动和启动和停止MySQL服务停止MySQL服务
    Debian中完全卸载MySQL
    dd,实现系统备份
    NeHe OpenGL教程 第十九课:粒子系统
    NeHe OpenGL教程 第十八课:二次几何体
  • 原文地址:https://www.cnblogs.com/liuzhen1995/p/6623786.html
Copyright © 2011-2022 走看看