蓝桥杯 2013年省赛真题 (Java 大学C组)

题型有点诡异,这个做完先做近几年的吧


#1 猜年龄

问题描述

美国数学家维纳(N.Wiener)智力早熟,11岁就上了大学。他曾在1935~1936年应邀来中国清华大学讲学。

一次,他参加某个重要会议,年轻的脸孔引人注目。于是有人询问他的年龄,他回答说:

“我年龄的立方是个4位数。我年龄的4次方是个6位数。这10个数字正好包含了从0到9这10个数字,每个都恰好出现1次。”

请你推算一下,他当时到底有多年轻。


答案提交

通过浏览器,直接提交他那时的年龄数字。
注意:不要提交解答过程,或其它的说明文字。


18

calcCode:

public class Test {
    
    

    public static void main(String[] args) {
    
    
        agent: for (int i = 11; i <= 300; i++) {
    
    
            long a = i * i * i, b = i * a;
            boolean[] marked = new boolean[10];
            do marked[(int)(a % 10)] = true;
            while ((a /= 10) > 0);
            do marked[(int)(b % 10)] = true;
            while ((b /= 10) > 0);
            for (int k = 0; k < 10; k++)
                if (!marked[k]) continue agent;
            System.out.println(i);
        }
    }
}

暴力解,我寻思着他总不能活三百多年吧


#2 组素数

问题描述

素数就是不能再进行等分的数。比如:2 3 5 7 11 等。
9 = 3 * 3 说明它可以3等分,因而不是素数。

我们国家在1949年建国。如果只给你 1 9 4 9 这4个数字卡片,可以随意摆放它们的先后顺序(但卡片不能倒着摆放啊,我们不是在脑筋急转弯!),那么,你能组成多少个4位的素数呢?

比如:1949,4919 都符合要求。


答案提交

请你提交:能组成的4位素数的个数,不要罗列这些素数!!
注意:不要提交解答过程,或其它的辅助说明文字。


6

calcCode:

public class Test {
    
    

    public static void main(String[] args) {
    
    
        int cnt = 0, num[] = {
    
     1, 4, 9, 9 };
        agent: do {
    
    
            int n = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
            for (int i = 2, k = (int)Math.sqrt(n); i <= k; i++)
                if (n % i == 0) continue agent;
                cnt++;
        } while (next(num));
        System.out.print(cnt);
    }

    static boolean next(int[] a) {
    
    
        int i = a.length - 2;
        while (i >= 0 && a[i] >= a[i + 1]) i--;
        if (i < 0) return false;
        int j = i + 1, t = a[i];
        while (j < a.length && a[j] > t) j++;
        a[i] = a[j - 1];
        a[j - 1] = t;
        Arrays.sort(a, i + 1, a.length);
        return true;
    }
}

反正用不了多少时间,就写个字典序,免得我全排列去重

接下来暴力解就完事了


#3 马虎的算式

问题描述

小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了。

有一次,老师出的题目是:36 x 495 = ?

他却给抄成了:396 x 45 = ?

但结果却很戏剧性,他的答案竟然是对的!!

因为 36 * 495 = 396 * 45 = 17820

类似这样的巧合情况可能还有很多,比如:27 * 594 = 297 * 54

假设 a b c d e 代表1~9不同的5个数字(注意是各不相同的数字,且不含0)

能满足形如: ab * cde = adb * ce 这样的算式一共有多少种呢?

请你利用计算机的优势寻找所有的可能,并回答不同算式的种类数。

满足乘法交换律的算式计为不同的种类,所以答案肯定是个偶数。


答案提交

答案直接通过浏览器提交。
注意:只提交一个表示最终统计种类数的数字,不要提交解答过程或其它多余的内容。


142

calcCode:

public class Test {
    
    

    public static void main(String[] args) {
    
    
        int cnt = 0;
        for (int a = 1; a < 10; a++)
            for (int b = 1; b < 10; b++) {
    
    
                if (a == b) continue;
                for (int c = 1; c < 10; c++) {
    
    
                    if (a == c || b == c) continue;
                    for (int d = 1; d < 10; d++) {
    
    
                        if (a == d || b == d || c == d) continue;
                        for (int e = 1; e < 10; e++) {
    
    
                            if (a == e || b == e || c == e || d == e) continue;
                            if ((a * 10 + b) * (c * 100 + d * 10 + e) == (a * 100 + d * 10 + b) * (c * 10 + e)) cnt++;
                        }
                    }
                }
            }
        System.out.print(cnt);
    }
}

一开始想用深搜,然后看式子是这么个诡异的东西

就直接暴力解完事了


#4 第39级台阶

问题描述

小明刚刚看完电影《第39级台阶》,离开电影院的时候,他数了数礼堂前的台阶数,恰好是39级!

站在台阶前,他突然又想着一个问题:

如果我每一步只能迈上1个或2个台阶。先迈左脚,然后左右交替,最后一步是迈右脚,也就是说一共要走偶数步。那么,上完39级台阶,有多少种不同的上法呢?

请你利用计算机的优势,帮助小明寻找答案。


答案提交

要求提交的是一个整数。
注意:不要提交解答过程,或其它的辅助说明文字。


51167078

calcCode:

public class Test {
    
    

    public static void main(String[] args) {
    
    
        System.out.print(dfs(0, 0));
    }

    static int dfs(int d, int v) {
    
    
        if (v >= 39) {
    
    
            if (v == 39 && 0 == (d & 1)) return 1;
            return 0;
        }
        return dfs(d + 1, v + 1) + dfs(d + 1, v + 2);
    }
}

在蓝桥官网可以找到一个课程,按里面的解法该题答案为 51167078 ,也就是最后一步是不论大小步的

理解是能理解,但我觉得还是有一定文字陷阱在里面的,可以加一句刚好上完 39 阶之类的描述

如果换我做,以我读填空题的各种程度,这题指定丢分
算了,我是个沙口


#5 有理数类

问题描述

有理数就是可以表示为两个整数的比值的数字。一般情况下,我们用近似的小数表示。但有些时候,不允许出现误差,必须用两个整数来表示一个有理数。

这时,我们可以建立一个“有理数类”,下面的代码初步实现了这个目标。为了简明,它只提供了加法和乘法运算。

class Rational
{
    
    
    private long ra;
    private long rb;
    
    private long gcd(long a, long b){
    
    
        if(b==0) return a;
        return gcd(b,a%b);
    }
    public Rational(long a, long b){
    
    
        ra = a;
        rb = b;    
        long k = gcd(ra,rb);
        if(k>1){
    
     //需要约分
            ra /= k;  
            rb /= k;
        }
    }
    // 加法
    public Rational add(Rational x){
    
    
        return ________________________________________;  //填空位置
    }
    // 乘法
    public Rational mul(Rational x){
    
    
        return new Rational(ra*x.ra, rb*x.rb);
    }
    public String toString(){
    
    
        if(rb==1) return "" + ra;
        return ra + "/" + rb;
    }
}
 
使用该类的示例:
    Rational a = new Rational(1,3);
    Rational b = new Rational(1,6);
    Rational c = a.add(b);
    System.out.println(a + "+" + b + "=" + c);

答案提交

请分析代码逻辑,并推测划线处的代码,通过网页提交
注意:仅把缺少的代码作为答案,千万不要填写多余的代码、符号或说明文字!!


new Rational(this.ra * x.rb + x.ra * this.rb, this.rb * x.rb);

该类构造方法已经实现了约分,直接通分就行

啊这,这是啥题型啊


#6 逆波兰表达式

问题描述

正常的表达式称为中缀表达式,运算符在中间,主要是给人阅读的,机器求解并不方便。

例如:3 + 5 * (2 + 6) - 1

而且,常常需要用括号来改变运算次序。

相反,如果使用逆波兰表达式(前缀表达式)表示,上面的算式则表示为:

- + 3 * 5 + 2 6 1

不再需要括号,机器可以用递归的方法很方便地求解。

为了简便,我们假设:

  1. 只有 + - * 三种运算符
  2. 每个运算数都是一个小于10的非负整数

下面的程序对一个逆波兰表示串进行求值。
其返回值为一个数组:其中第一元素表示求值结果,第二个元素表示它已解析的字符数。

    static int[] evaluate(String x)
    {
    
    
        if(x.length()==0) return new int[] {
    
    0,0};
        
        char c = x.charAt(0);
        if(c>='0' && c<='9') return new int[] {
    
    c-'0',1};
        
        int[] v1 = evaluate(x.substring(1));
        int[] v2 = __________________________________________;  //填空位置
        
        int v = Integer.MAX_VALUE;
        if(c=='+') v = v1[0] + v2[0];
        if(c=='*') v = v1[0] * v2[0];
        if(c=='-') v = v1[0] - v2[0];
        
        return new int[] {
    
    v,1+v1[1]+v2[1]};
    }

答案提交

请分析代码逻辑,并推测划线处的代码,通过网页提交
注意:仅把缺少的代码作为答案,千万不要填写多余的代码、符号或说明文字!!


evaluate(x.substring(1 + v1[1]));

一般我们书写的算式都是 中缀表达式

二叉树形式也就是

         -
  +       1
3   *
   5  +
	 2  6

运算符是非叶节点,运算数是叶子节点

熟悉二叉树的同学看到这句话大概就能明白这道题怎么解了


#7 核桃的数量

问题描述

小张是软件项目经理,他带领3个开发组。工期紧,今天都在加班呢。为鼓舞士气,小张打算给每个组发一袋核桃(据传言能补脑)。他的要求是:

  1. 各组的核桃数量必须相同
  2. 各组内必须能平分核桃(当然是不能打碎的)
  3. 尽量提供满足1,2条件的最小数量(节约闹革命嘛)

资源约定

峰值内存消耗(含虚拟机) < 64M
CPU消耗 < 1000ms


输入格式

a b c
a,b,c都是正整数,表示每个组正在加班的人数,用空格分开(a,b,c<30)


输出格式

一个正整数,表示每袋核桃的数量。


测试样例1

Input:
2 4 5

Output:
20

测试样例2

Input:
3 1 1

Output:
3

code:

import java.io.*;

public class Main {
    
    

    public static void main(String[] args) {
    
    
        InputReader in = new InputReader(System.in);
        int a = in.nextInt(), b = in.nextInt(), c = in.nextInt();
        System.out.print(lcm(lcm(a, b), c));
    }

    static int gcd(int a, int b) {
    
     return b == 0? a: gcd(b, a % b); }

    static int lcm(int a, int b) {
    
     return a * b / gcd(a, b); }

    static class InputReader {
    
    

        byte[] buff;
        int next, len;
        InputStream in;

        InputReader(InputStream in) {
    
     this(in, 8192); }

        InputReader(InputStream in, int buffSize) {
    
    
            this.buff = new byte[buffSize];
            this.next = this.len = 0;
            this.in = in;
        }

        int getByte() {
    
    
            if (next >= len)
                try {
    
    
                    next = 0;
                    len = in.read(buff);
                } catch (IOException e) {
    
     }
            return buff[next++];
        }

        int nextInt() {
    
    
            int n = 0, c = getByte();
            while (c < '0' || c > '9') c = getByte();
            while (c >='0' && c <='9') {
    
    
                n = n * 10 + (c & 0xf);
                c = getByte();
            }
            return n;
        }
    }
}

最小公倍数写脸上了


#8 打印十字图

问题描述

小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示(可参见p1.jpg)

..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..

对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数。

为了能准确比对空白的数量,程序要求对行中的空白以句点(.)代替。


资源约定

峰值内存消耗(含虚拟机) < 64M
CPU消耗 < 1000ms


输入格式

一个正整数 n (n<30) 表示要求打印图形的层数


输出格式

对应包围层数的该标志。


测试样例1

Input:
1

Output:
..$$$$$..
..$...$..
$$$.$.$$$
$...$...$
$.$$$$$.$
$...$...$
$$$.$.$$$
..$...$..
..$$$$$..

测试样例2

Input:
3

Output:
..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..

code:

import java.io.*;
import java.util.ArrayDeque;
import java.util.Queue;

public class Main {
    
    

    public static void main(String[] args) throws IOException {
    
    
        int n = nextInt(System.in), mid = (n << 1) + 2, size = mid << 1;
        byte[][] map = new byte[size + 1][];
        int list[] = {
    
     -1, 0, 0, -1, -1, -1 };
        boolean[][] marked = new boolean[mid + 1][mid + 1];
        for (int i = 0; i <= mid; i++) {
    
    
            map[i] = map[size - i] = new byte[size + 2];
            map[i][size + 1] = '\n';
        }
        Queue<Item> queue = new ArrayDeque();
        queue.offer(new Item(mid, mid, (byte)'$'));
        queue.offer(new Item(mid - 1, mid, (byte)'$'));
        queue.offer(new Item(mid - 2, mid, (byte)'$'));
        queue.offer(new Item(mid, mid - 1, (byte)'$'));
        queue.offer(new Item(mid, mid - 2, (byte)'$'));
        marked[mid][mid] = marked[mid - 1][mid] = marked[mid - 2][mid] = marked[mid][mid - 1] = marked[mid][mid - 2] = true;
        while (!queue.isEmpty()) {
    
    
            Item now = queue.poll();
            map[now.i][now.j] = map[now.i][size - now.j] = now.v;
            int i, j, c = 0;
            while (c < 6) {
    
    
                i = now.i + list[c++];
                j = now.j + list[c++];
                if (i >= 0 && j >= 0 && !marked[i][j] ) {
    
    
                    queue.offer(new Item(i, j, now.other()));
                    marked[i][j] = true;
                }
            }
        }
        map[0][0] = map[size][size] = '.';
        for (int i = 0; i <= size; i++)
            System.out.write(map[i]);
    }

    static class Item {
    
    

        int i, j;
        byte v;

        Item(int i, int j, byte v) {
    
    
            this.i = i;
            this.j = j;
            this.v = v;
        }

        byte other() {
    
    
            return (byte)(this.v == '$'? '.': '$');
        }
    }

    static int nextInt(InputStream in) throws IOException {
    
    
        int n = 0, c = in.read();
        while (c < '0' || c > '9') c = in.read();
        while (c >='0' && c <='9') {
    
    
            n  = n * 10 + (c & 0xf);
            c = in.read();
        }
        return n;
    }
}

写了个列队,一层一层的生成

没啥好说的


#9 买不到的数目

问题描述

小明开了一家糖果店。他别出心裁:把水果糖包成4颗一包和7颗一包的两种。糖果不能拆包卖。

小朋友来买糖的时候,他就用这两种包装来组合。当然有些糖果数目是无法组合出来的,比如要买 10 颗糖。

你可以用计算机测试一下,在这种包装情况下,最大不能买到的数量是17。大于17的任何数字都可以用4和7组合出来。

本题的要求就是在已知两个包装的数量时,求最大不能组合出的数字。


资源约定

峰值内存消耗(含虚拟机) < 64M
CPU消耗 < 3000ms


输入格式

两个正整数,表示每种包装中糖的颗数(都不多于1000)


输出格式

一个正整数,表示最大不能买到的糖数


测试样例1

Input:
4 7

Output:
17

测试样例2

Input:
3 5

Output:
7

数据规模与约定

不需要考虑无解的情况


code:

import java.io.*;

public class Main {
    
    

    public static void main(String[] args) throws IOException {
    
    
        int a = nextInt(System.in), b = nextInt(System.in);
        int max = lcm(a, b), cnt = 0;
        boolean[] dp = new boolean[max];
        dp[a] = dp[b] = true;
        for (int i = 1; i < max; i++) {
    
    
            if (i - a >= 0) dp[i] = dp[i] | dp[i - a];
            if (i - b >= 0) dp[i] = dp[i] | dp[i - b];
            if (!dp[i]) cnt = i;
        }
        System.out.print(cnt);
    }

    static int gcd(int a, int b) {
    
     return b == 0? a: gcd(b, a % b); }

    static int lcm(int a, int b) {
    
     return a * b / gcd(a, b); }

    static int nextInt(InputStream in) throws IOException {
    
    
        int n = 0, c = in.read();
        while (c < '0' || c > '9') c = in.read();
        while (c >='0' && c <='9') {
    
    
            n  = n * 10 + (c & 0xf);
            c = in.read();
        }
        return n;
    }
}

不需要考虑无解的情况,也就是到达一个临界点后两数必定能组成临界点后的全部数字,直接遍历之前即可


#10 剪格子

问题描述

如图p1.jpg所示,3 x 3 的格子中填写了一些整数。

我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60。

本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以分割为两个部分,使得这两个区域的数字和相等。

如果存在多种解答,请输出包含左上角格子的那个区域包含的格子的最小数目。
如果无法分割,则输出 0。


资源约定

峰值内存消耗(含虚拟机) < 64M
CPU消耗 < 5000ms


输入格式

程序先读入两个整数 m n 用空格分割 (m,n<10)
表示表格的宽度和高度
接下来是n行,每行m个正整数,用空格分开。每个整数不大于10000


输出格式

在所有解中,包含左上角的分割区可能包含的最小的格子数目。


测试样例1

Input:
3 3
10 1 52
20 30 1
1 2 3

Output:
3

测试样例2

Input:
4 3
1 1 1 1
1 30 80 2
1 1 1 100

Output:
10

code:

import java.io.*;

public class Main {
    
    

    static boolean[][] marked;
    static int n, m, cnt, sum, map[][];
    static int next[] = {
    
     1, 0, 0, 1, -1, 0, 0, -1};

    public static void main(String[] args) throws IOException {
    
    
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        m = (int)in.nval;
        in.nextToken();
        n = (int)in.nval;
        map = new int[n][m];
        marked = new boolean[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
    
    
                in.nextToken();
                sum += map[i][j] = (int)in.nval;
            }
        if (1 == (sum & 1)) System.out.print("0");
        else {
    
    
            sum /= 2;
            System.out.print(dfs(0, 0, map[0][0]));
        }
    }

    static int dfs(int x, int y, int v) {
    
    
        if (v > sum) return 0;
        if (v == sum) return 1;
        int i, j, c = 0;
        while (c < 8) {
    
    
            i = x + next[c++];
            j = y + next[c++];
            if (i >= 0 && i < n && j >= 0 && j < m && !marked[i][j]) {
    
    
                marked[i][j] = true;
                int res = dfs(i, j, v + map[i][j]);
                if (res > 0) return res + 1;
                marked[i][j] = false;
            }
        }
        return 0;
    }
}

完事,睡觉

猜你喜欢

转载自blog.csdn.net/qq_43449564/article/details/109003960