2016年第七届蓝桥杯javaB组 试题 答案 解析

1.煤球数目

  • 有一堆煤球,堆成三角棱锥形。具体:
  • 第一层放1个,
  • 第二层3个(排列成三角形),
  • 第三层6个(排列成三角形),
  • 第四层10个(排列成三角形),
  • ....
  • 如果一共有100层,共有多少个煤球?
  • 请填表示煤球总数目的数字。
  • 注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
public class One {
    
    public static void main(String[] args) {
        int sum = 0;
        for(int i = 1; i <= 100; i++){
            sum += count(i);
        }
        System.out.println(sum);
    }
    
    public static int count(int num){
        int sum = 0;
        for(int i = 1; i <= num; i++){
            sum += i;
        }
        return sum;
    }
}

答案: 171700


2.生日蜡烛

  • 某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛。
  • 现在算起来,他一共吹熄了236根蜡烛。
  • 请问,他从多少岁开始过生日party的?
  • 请填写他开始过生日party的年龄数。
  • 注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
public class Two {
    
    public static void main(String[] args) {
        for(int i = 0; i < 100; i++){
            for(int j = 0; j < 100; j++){
                count(i, j);
            }
        }
    }
    
    public static int count(int start, int end){
        if(start >= end){
            return 0;
        }
        int sum = 0;
        for(int i = start; i < (end - start + 1); i++){
            sum += i;
        }
        if(sum == 236){
            for(int i = start; i < (end - start + 1); i++){
                System.out.print(i + " ");
            }
        }
        return sum;
    }
}

答案: 26


3.凑算式

     B      DEF
A + --- + ------- = 10
     C      GHI
  • 这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。
  • 比如:
  • 6+8/3+952/714 就是一种解法,
  • 5+3/1+972/486 是另一种解法。
  • 这个算式一共有多少种解法?
public class Three {

    public static void main(String[] args) {
        int num = 0;
        for(float a = 1; a <= 9; a++){
            for(float b = 1; b <= 9; b++){
                if(a == b) continue;
                for(float c = 1; c <= 9; c++){
                    if(a == c || c == b) continue;
                    for(float d = 1; d <= 9; d++){
                        if(d == a || d == b || d == c) continue;
                        for(float e = 1; e <= 9; e++){
                            if(e == a || e == b || e == c || e == d) continue;
                            for(float f = 1; f <= 9; f++){
                                if(f == a || f == b || f == c || f == d || f == e) continue;
                                for(float g = 1; g <= 9; g++){
                                    if(g == a || g == b || g == c || g == d || g == e || g == f) continue;
                                    for(float h = 1; h <= 9; h++){
                                        if(h == a || h == b || h == c || h == d || h == e || h == f || h == g) continue;
                                        for(float i = 1; i <= 9; i++){
                                            if(i == a || i == b || i == c || i == d || i == e || i == f || i == g || i == h) continue;
                                            if(count(a, b, c, d, e, f, g, h, i) == 10){
                                                System.out.println(++num);
                                            }
                                        }   
                                    }   
                                }   
                            }   
                        }   
                    }   
                }   
            }
        }
    }
    
    public static float count(float a, float b, float c, float d, float e, float f, float g, float h, float i){
        
        return a + (b / c) + ((d * 100 + e * 10 + f) / (g * 100 + h * 10 + i));
    }
}

答案: 29


4. 分小组

  • 9名运动员参加比赛,需要分3组进行预赛。
  • 有哪些分组的方案呢?
  • 我们标记运动员为 A,B,C,... I
  • 下面的程序列出了所有的分组方法。
该程序的正常输出为:
ABC DEF GHI
ABC DEG FHI
ABC DEH FGI
ABC DEI FGH
ABC DFG EHI
ABC DFH EGI
ABC DFI EGH
ABC DGH EFI
ABC DGI EFH
ABC DHI EFG
ABC EFG DHI
ABC EFH DGI
ABC EFI DGH
ABC EGH DFI
ABC EGI DFH
ABC EHI DFG
ABC FGH DEI
ABC FGI DEH
ABC FHI DEG
ABC GHI DEF
ABD CEF GHI
ABD CEG FHI
ABD CEH FGI
ABD CEI FGH
ABD CFG EHI
ABD CFH EGI
ABD CFI EGH
ABD CGH EFI
ABD CGI EFH
ABD CHI EFG
ABD EFG CHI
..... (以下省略,总共560行)。

public class A
{
    public static String remain(int[] a)
    {
        String s = "";
        for(int i=0; i<a.length; i++){
            if(a[i] == 0) s += (char)(i+'A');
        }   
        return s;
    }
    
    public static void f(String s, int[] a)
    {
        for(int i=0; i<a.length; i++){
            if(a[i]==1) continue;
            a[i] = 1;
            for(int j=i+1; j<a.length; j++){
                if(a[j]==1) continue;
                a[j]=1;
                for(int k=j+1; k<a.length; k++){
                    if(a[k]==1) continue;
                    a[k]=1;
                    System.out.println(__________________________________);  //填空位置
                    a[k]=0;
                }
                a[j]=0;
            }
            a[i] = 0;
        }
    }
    
    public static void main(String[] args)
    {
        int[] a = new int[9];       
        a[0] = 1;
        
        for(int b=1; b<a.length; b++){
            a[b] = 1;
            for(int c=b+1; c<a.length; c++){
                a[c] = 1;
                String s = "A" + (char)(b+'A') + (char)(c+'A');
                f(s,a);
                a[c] = 0;
            }
            a[b] = 0;
        }
    }
}
  • 仔细阅读代码,填写划线部分缺少的内容。

    答案: s + " " + (char)(i + 'A') + (char)(j + 'A') + (char)(k + 'A') + " " + remain(a)


5.抽签

  • X星球要派出一个5人组成的观察团前往W星。
  • 其中:
  • A国最多可以派出4人。
  • B国最多可以派出2人。
  • C国最多可以派出2人。
  • ....
  • 那么最终派往W星的观察团会有多少种国别的不同组合呢?
  • 下面的程序解决了这个问题。
  • 数组a[] 中既是每个国家可以派出的最多的名额。
  • 程序执行结果为:
DEFFF
CEFFF
CDFFF
CDEFF
CCFFF
CCEFF
CCDFF
CCDEF
BEFFF
BDFFF
BDEFF
BCFFF
BCEFF
BCDFF
BCDEF
....
(以下省略,总共101行)


public class A
{
    public static void f(int[] a, int k, int n, String s)
    {
        if(k==a.length){ 
            if(n==0) System.out.println(s);
            return;
        }
        
        String s2 = s;
        for(int i=0; i<=a[k]; i++){
            _____________________________;   //填空位置
            s2 += (char)(k+'A');
        }
    }
    
    public static void main(String[] args)
    {
        int[] a = {4,2,2,1,1,3};
        
        f(a,0,5,"");
    }
}
  • 仔细阅读代码,填写划线部分缺少的内容。

    答案: f(a, k + 1, n - i, s2);


6.方格填数

  • 如下的10个格子
   +--+--+--+
   |  |  |  |
+--+--+--+--+
|  |  |  |  |
+--+--+--+--+
|  |  |  |
+--+--+--+
  • 填入0~9的数字。要求:连续的两个数字不能相邻。
    (左右、上下、对角都算相邻)
  • 一共有多少种可能的填数方案?
  • 请填写表示方案数目的整数。

这道题的递归策略类似八皇后问题, 也就说当来到第一个格子后, 下一个要填的数不是连接着下一个位置的, 也就是说每个位置要填的数字来自于整个样本, 而且要注意回溯, 保证其他递归过程没有出错.

public class Six {
    
    public static int[][] dp = new int[5][6];
    public static int[] arr = new int[10];
    public static int res = 0;
    
    public static void main(String[] args) {
        initDp();
        process(1, 2);
        System.out.println(res);
    }
    
    public static void process(int x, int y){
        if(x == 3 && y == 4){
            res++;
            return;
        }
        for(int n = 0; n <= 9; n++){
            if(arr[n] == 0 && canFill(x, y, n)){
                dp[x][y] = n;
                arr[n] = 1;
                if(y == 4){
                    process(x + 1, 1);
                }else{
                    process(x, y + 1);
                }
                arr[n] = 0;
                dp[x][y] = -11;
            }
        }
    }
    
    public static boolean canFill(int x, int y, int n){
        boolean ans = true;
        for(int i = -1; i <= 1; i++){
            for(int j = -1; j <= 1; j++){
                if(i == 0 && j == 0) continue;
                if(Math.abs(dp[x + i][y + j] - n) <= 1){
                    ans = false;
                }
            }
        }
        return ans;
    }
    
    public static void initDp(){
        for(int i = 0; i < 5; i++){
            for(int j = 0; j < 6; j++){
                dp[i][j] = -11;
            }
        }
    }
}

答案: 1580


7.剪邮票

  • 如【图1.jpg】, 有12张连在一起的12生肖的邮票
  • 现在你要从中剪下5张来,要求必须是连着的
  • (仅仅连接一个角不算相连)
  • 比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。
  • 请你计算,一共有多少种不同的剪取方法。
  • 请填写表示方案数目的整数。

首先通过5层for循环枚举选出5数, 难点在于判断这5个数是否是相连的, 可以利用图中标有编号的特性进行判断. 可以直观地看到一个数上面联通的数可-4得到, 下面的数可+4得到, 同理左边是-1, 右边是+1. 但是, 有特殊情况, 比如4+1为5, 理论上4的右边是5, 但是由于换行的原因, 4和5不是联通的. 但是这个方法是好的, 可以通过修改图中的编号继续使用这个方法.

可以把图改成: 
1  2  3  4 
6  7  8  9
10 11 12 13
public class Seven {

    public static int res = 0;
    public static int[] arr = {1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14};
    public static int[] p = new int[5];
    public static boolean[] visit = new boolean[5];
    public static int[] c = {5, -5, 1, -1};
    
    public static void main(String[] args) {
        for(int a = 0; a < 12; a++){
            for(int b = a + 1; b < 12; b++){
                for(int c = b + 1; c < 12; c++){
                    for(int d = c + 1; d < 12; d++){
                        for(int e = d + 1; e < 12; e++){
                            p[0] = arr[a];
                            p[1] = arr[b];
                            p[2] = arr[c];
                            p[3] = arr[d];
                            p[4] = arr[e];
                            process(0);
                            boolean flag = true;
                            for(int i = 0; i < 5; i++){
                                if(!visit[i]){
                                    flag = false;
                                }
                            }
                            if(flag){
                                res++;
                            }
                            cleanVisit();
                        }
                    }
                }
            }
        }
        System.out.println(res);
    }
    
    public static void process(int n){
        for(int i = 0; i < 4; i++){
            int t = p[n] + c[i];
            if(t < 1 || t > 14 || t == 5 || t == 10){
                continue;
            }
            for(int j = 0; j < 5; j++){
                if(!visit[j] && p[j] == t){
                    visit[j] = true;
                    process(j);
                }
            }
        }
    }
    
    public static void cleanVisit(){
        for(int i = 0; i < 5; i++){
            visit[i] = false;
        }
    }
}

答案: 116

扫描二维码关注公众号,回复: 5552353 查看本文章

8.四平方和

  • 四平方和定理,又称为拉格朗日定理:
  • 每个正整数都可以表示为至多4个正整数的平方和。
  • 如果把0包括进去,就正好可以表示为4个数的平方和。
  • 比如:
  • 5 = 0^2 + 0^2 + 1^2 + 2^2
  • 7 = 1^2 + 1^2 + 1^2 + 2^2
  • (^符号表示乘方的意思)
  • 对于一个给定的正整数,可能存在多种平方和的表示法。
  • 要求你对4个数排序:
  • 0 <= a <= b <= c <= d
  • 并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法
  • 程序输入为一个正整数N (N<5000000)
  • 要求输出4个非负整数,按从小到大排序,中间用空格分开
例如,输入:
5
则程序应该输出:
0 0 1 2

再例如,输入:
12
则程序应该输出:
0 2 2 2

再例如,输入:
773535
则程序应该输出:
1 1 267 838

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

这道题是常规的暴力枚举题, 难在如何降低时间复杂度跑过所有的用例, 如果a, b, c, d每个数都穷举以便, 按照最大的输入5000000, 每个数要枚举的次数为10^3级, 一共就是10^12. 太大了, 会超时. 为了能减少次数, 把枚举分成两半, 一半的数据是a * a + b * b, 这一半的数据用哈希表存储, 另外一半的数据是c * c + d * d = n - a * a + b * b. 这样能把枚举的规模缩小一半.

public class Eight {
    
    static HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        for(int c = 0; c * c <= n / 2; c++){
            for(int d = c; c * c + d * d <= n; d++){
                if(!map.containsKey(c * c + d * d)){
                    map.put(c * c + d * d, c);
                }
            }
        }
        for(int a = 0; a * a <= n / 4; a++){
            for(int b = a; a * a + b * b <= n; b++){
                if(map.containsKey(n - a * a - b * b)){
                    int c = map.get(n - a * a - b * b);
                    int d = (int) Math.sqrt(n - a * a - b * b - c * c);
                    System.out.println(a + " " + b + " " + c + " " + d);
                    return;
                }
            }
        }
    }}

9.取球博弈

  • 两个人玩取球的游戏。
  • 一共有N个球,每人轮流取球,每次可取集合{n1,n2,n3}中的任何一个数目。
  • 如果无法继续取球,则游戏结束。
  • 此时,持有奇数个球的一方获胜。
  • 如果两人都是奇数,则为平局。
  • 假设双方都采用最聪明的取法,
  • 第一个取球的人一定能赢吗?
  • 试编程解决这个问题。
  • 输入格式:
  • 第一行3个正整数n1 n2 n3,空格分开,表示每次可取的数目 (0<n1,n2,n3<100)
  • 第二行5个正整数x1 x2 ... x5,空格分开,表示5局的初始球数(0<xi<1000)
  • 输出格式:
  • 一行5个字符,空格分开。分别表示每局先取球的人能否获胜。
  • 能获胜则输出+,
  • 次之,如有办法逼平对手,输出0,
  • 无论如何都会输,则输出-
例如,输入:
1 2 3
1 2 3 4 5

程序应该输出:
+ 0 + 0 -

再例如,输入:
1 4 5
10 11 12 13 15

程序应该输出:
0 - 0 + +

再例如,输入:
2 3 5
7 8 9 10 11

程序应该输出:
+ 0 0 0 0

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

思路

  • 首先要明确每一局的情况, 假设一局有x个球, 取球情况的数组为n.
  • 在每一局中, 某一方取球都会根据取球情况数组进行试探, 如上图.
  • 在每次递归中, 需要的参数有当前剩下的球, 我摸的球数, 对手摸的球数.
  • 如何才能判断一局的胜负? 在所有情况中, 只要对手输过, 结果就是赢; 如果对手没输过, 但是平过, 结果就是平; 否则, 结果就是输. (因为题目条件: 双方都采取最聪明的办法)
  • 最大的难点在于在于递归的过程中, 不是每次都是我摸, 而是我摸一次, 对手摸一次, 这里恰好就是这种类型递归的魅力所在.
  • 具体落实到代码中
public class QuQiuBoYi {
    
    public static int[] n = new int[3];
    public static int[] m = new int[5];
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for(int i = 0; i < 3; i++){
            n[i] = sc.nextInt();
        }
        Arrays.sort(n);
        for(int i = 0; i < 5; i++){
            m[i] = sc.nextInt();
            char res = process(m[i], 0, 0);//每输入一局的球数, 就计算一局
            System.out.print(res + " ");
        }
    }
    
    public static char process(int num, int me, int you){
        if(num < n[0]){//没球可摸,进行结算
            if((me & 1) != 0 && (you & 1) == 0){//我奇数, 对手偶数, 赢
                return '+';
            }else if((me & 1) == 0 && (you & 1) != 0){//我偶数, 对手奇数, 输
                return '-';
            }else{
                return '0';//平
            }
        }
        boolean draw = false;
        for(int i = 0; i < 3; i++){
            if(num >= n[i]){
                //重点!假设当前这轮是我摸球, 在我摸完球me+n[i]后, 就到对手摸, me和you的位置转换, 设定中间的参数是摸球的一方
                char res = process(num - n[i], you, me + n[i]);
                if(res == '-'){//由于下一轮是对手摸球, 如果他输了
                    return '+';//我就赢了
                }else if(res == '0'){//如果有平局, 要记录下来
                    draw = true;
                }
            }
        }
        return draw ? '0' : '-';
    }
}

优化

  • 作为编程大题, 如果不优化是过不了极端的数据样本的.
  • 这题可以用数组记录重复出现的情况, 进行记忆型递归, 用空间换时间.
  • 问题来了: me和you是不断交换的, 怎样才算是一种情况?
  • 在base case中, 最后决定输赢的, 不是你我手上有多少个球, 而是你我的球数的奇偶情况, 所以我们把每一种情况记录为: 剩余的球数和双方球数的奇偶情况.
public class Nine {
    
    public static int[] n = new int[3];
    public static int[] m = new int[5];
    public static char[][][] cache = new char[1000][2][2];
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for(int i = 0; i < 3; i++){
            n[i] = sc.nextInt();
        }
        Arrays.sort(n);
        for(int i = 0; i < 5; i++){
            m[i] = sc.nextInt();
            char res = process(m[i], 0, 0);
            System.out.print(res + " ");
        }
    }
    
    public static char process(int num, int me, int you){
        if(num < n[0]){
            if((me & 1) != 0 && (you & 1) == 0){
                return '+';
            }else if((me & 1) == 0 && (you & 1) != 0){
                return '-';
            }else{
                return '0';//平
            }
        }
        if(cache[num][me][you] != '\0'){
            return cache[num][me][you]; 
        }
        boolean draw = false;
        for(int i = 0; i < 3; i++){
            if(num >= n[i]){
                char res = process(num - n[i], you, ((me + n[i]) & 1) == 0 ? 0 : 1);//只记录奇偶情况
                if(res == '-'){
                    cache[num][me][you] = '+';
                    return '+';
                }else if(res == '0'){
                    draw = true;
                }
            }
        }
        if(draw){
            cache[num][me][you] = '0';
            return '0';
        }else{
            cache[num][me][you] = '-';
            return '-';
        }
    }
}

10.压缩变换

  • 小明最近在研究压缩算法。
  • 他知道,压缩的时候如果能够使得数值很小,就能通过熵编码得到较高的压缩比。
  • 然而,要使数值很小是一个挑战。
  • 最近,小明需要压缩一些正整数的序列,这些序列的特点是,后面出现的数字很大可能是刚出现过不久的数字。对于这种特殊的序列,小明准备对序列做一个变换来减小数字的值。
  • 变换的过程如下:
  • 从左到右枚举序列,每枚举到一个数字,如果这个数字没有出现过,刚将数字变换成它的相反数,如果数字出现过,则看它在原序列中最后的一次出现后面(且在当前数前面)出现了几种数字,用这个种类数替换原来的数字。
  • 比如,序列(a1, a2, a3, a4, a5)=(1, 2, 2, 1, 2)在变换过程为:
  • a1: 1未出现过,所以a1变为-1;
  • a2: 2未出现过,所以a2变为-2;
  • a3: 2出现过,最后一次为原序列的a2,在a2后、a3前有0种数字,所以a3变为0;
  • a4: 1出现过,最后一次为原序列的a1,在a1后、a4前有1种数字,所以a4变为1;
  • a5: 2出现过,最后一次为原序列的a3,在a3后、a5前有1种数字,所以a5变为1。
  • 现在,给出原序列,请问,按这种变换规则变换后的序列是什么。
输入格式:
输入第一行包含一个整数n,表示序列的长度。
第二行包含n个正整数,表示输入序列。

输出格式:
输出一行,包含n个数,表示变换后的序列。

例如,输入:
5
1 2 2 1 2

程序应该输出:
-1 -2 0 1 1

再例如,输入:
12
1 1 2 3 2 3 1 2 2 2 3 1

程序应该输出:
-1 0 -2 -3 1 1 2 2 0 0 2 2

数据规模与约定
对于30%的数据,n<=1000;
对于50%的数据,n<=30000;
对于100%的数据,1 <=n<=100000,1<=ai<=10^9

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

按照题目的思路, 使用HashMap记录数的下标, 使用HashSet记录不同种的的数字, 时间复杂度O(N^2), 暴力做法能得30分.

public class Ten {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] arr = new long[n];
        for(int i = 0; i < arr.length; i++){
            arr[i] = sc.nextInt();
        }
        long[] former = Arrays.copyOf(arr, arr.length);
        HashMap<Long, Integer> map = new HashMap<Long, Integer>();
        for(int i = 0; i < arr.length; i++){
            if(!map.containsKey(arr[i])){
                map.put(arr[i], i);
                arr[i] = -arr[i];
            }else{
                int types = getTypes(map.get(arr[i]), i, former);
                map.put(arr[i], i);
                arr[i] = types;
            }
        }
        for(int i = 0; i < arr.length; i++){
            System.out.print(arr[i] + " ");
        }
    }
    
    public static int getTypes(int begin, int last, long[] former){
        HashSet<Long> set = new HashSet<Long>();
        int index = begin + 1;
        while(index < last){
            set.add(former[index++]);
        }
        return set.size();
    }
}

优化

  • 未完待续...

猜你喜欢

转载自www.cnblogs.com/tanshaoshenghao/p/10542723.html