第九届蓝桥杯 2018年国赛真题 (Java 大学B组)


希望决赛题目不搞我

先挂


#1 三角形面积

本题满分: 13分


问题描述

已知三角形三个顶点在直角坐标系下的坐标分别为:
(2.3, 2.5)
(6.4, 3.1)
(5.1, 7.2)

求该三角形的面积。


答案提交

注意,要提交的是一个小数形式表示的浮点数。
要求精确到小数后3位,如不足3位,需要补零。


8.795


calcCode:

public class Test {
	
	public static void main(String[] args) {
		Point X = new Point(2.3, 2.5),
			  Y = new Point(6.4, 3.1),
			  Z = new Point(5.1, 7.2);
		double a = Point.distance(X, Y),
			   b = Point.distance(X, Z),
			   c = Point.distance(Y, Z),
			   p = (a + b + c) / 2;
		System.out.printf("%.3f\n", Math.sqrt(p * (p - a) * (p - b) * (p - c)));
	}
	
	static class Point {
		
		double a, b;
		
		Point(double a, double b) {
			this.a = a;
			this.b = b;
		}
		
		static double distance(Point a, Point b) { return Math.sqrt((a.a - b.a) * (a.a - b.a) + (a.b - b.b) * (a.b - b.b)); }
	}
}

海伦公式


#2 最大乘积

本题满分: 39分


问题描述

把 1~9 这9个数字分成两组,中间插入乘号,
有的时候,它们的乘积也只包含1~9这9个数字,而且每个数字只出现1次。

比如:
984672 * 351 = 345619872
98751 * 3462 = 341875962
9 * 87146325 = 784316925

符合这种规律的算式还有很多,请你计算在所有这些算式中,乘积最大是多少?


答案提交

注意,需要提交的是一个整数,表示那个最大的积,不要填写任何多余的内容。
(只提交乘积,不要提交整个算式)


839542176


calcCode:

public class Test {

    public static void main(String[] args) {
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int add = 0, mul = 1, max = 0;
        for (int i = 0; i < 9; i++) {
            add += arr[i];
            mul *= arr[i];
        }
        do
            for (int i = 0, l = 0, r, v, a, m; i < 9; i++) {
                l = l * 10 + arr[i];
                r = 0;
                for (int j = i + 1; j < 9; j++) r = r * 10 + arr[j];
                v = l * r;
                if (v > max) {
                    m = 1; a = 0;
                    do {
                        a += v % 10;
                        m *= v % 10;
                    } while ((v /= 10) > 0);
                    if (m == mul && a == add) max = l * r;
                }
            }
        while (next(arr));
        System.out.print(max);
    }

    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;
        java.util.Arrays.sort(a, i + 1, a.length);
        return true;
    }
}

声明一下,少了一步效验

不优雅,但实用


#3 全排列

本题满分: 27分


问题描述

对于某个串,比如:“1234”,求它的所有全排列。
并且要求这些全排列一定要按照字母的升序排列。
对于“1234”,应该输出(一共4!=24行):

1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321

下面是实现程序,请仔细分析程序逻辑,并填写划线部分缺少的代码。

// 轮换前k个,再递归处理
import java.util.*;
public class A
{
	static void permu(char[] data, int cur){
		if(cur==data.length-1){
			System.out.println(new String(data));
			return;
		}
		
		for(int i=cur; i<data.length; i++){
			char tmp = data[i]; 
			for(int j=i-1; j>=cur; j--) data[j+1] = data[j];
			data[cur] = tmp;			

			permu(data, cur+1);			

			tmp = data[cur]; 
			__________________________________________ ;
			data[i] = tmp;			
		}
	}
	
	static void permu(String x){
		permu(x.toCharArray(),0);
	}
	
	public static void main(String[] args){
		permu("1234");
	}
}

答案提交

请注意:只需要填写划线部分缺少的内容,不要抄写已有的代码或符号。


for (int j =cur; j <i; j++) data[j] = data[j+1];


#4 整理玩具

时间限制: 1.0s 内存限制: 256.0MB 本题满分: 45 分


问题描述

小明有一套玩具,一共包含NxM个部件。这些部件摆放在一个包含NxM个小格子的玩具盒中,每个小格子中恰好摆放一个部件。

每一个部件上标记有一个0~9的整数,有可能有多个部件标记相同的整数。

小明对玩具的摆放有特殊的要求:标记相同整数的部件必须摆在一起,组成一个矩形形状。

如以下摆放是满足要求的:

00022
00033
44444  

12244
12244
12233

01234
56789

以下摆放不满足要求:

11122
11122
33311

111111
122221
122221
111111

11122
11113
33333

给出一种摆放方式,请你判断是否符合小明的要求。


输入格式

输入包含多组数据。
第一行包含一个整数T,代表数据组数。 (1 <= T <= 10)
以下包含T组数据。
每组数据第一行包含两个整数N和M。 (1 <= N, M <= 10)
以下包含N行M列的矩阵,代表摆放方式。


输出格式

对于每组数据,输出YES或者NO代表是否符合小明的要求。


测试样例1

Input:
3
3 5
00022
00033
44444
3 5
11122
11122
33311
2 5
01234
56789

Output:
YES
NO
YES

code:

import java.io.InputStream;
import java.io.IOException;
import java.io.PrintWriter;

public class Main {

    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int[][] map = new int[10][10];
        int[] cnt = new int[128];
        int c = in.nextInt();
        while (c-- > 0) {
            for (int i = '0'; i <= '9'; i++) cnt[i] = 0;
            int n = in.nextInt(), m = in.nextInt();
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    cnt[map[i][j] = in.nextChar()]++;
            boolean flag = true;
            agent: for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++) {
                    if (map[i][j] == 0) continue;
                    int col = i + 1, row = j + 1;
                    int val = map[i][j];
                    while (col < n && map[col][j] == val) col++;
                    while (row < m && map[i][row] == val) row++;
                    if ((col - i) * (row - j) != cnt[val]) {
                        flag = false;
                        break agent;
                    }
                    for (int k = i; k < col; k++)
                        for (int g = j; g < row; g++)
                            if (map[k][g] != val) {
                                flag = false;
                                break agent;
                            } else map[k][g] = 0;
                }
            out.println(flag? "YES": "NO");
        }
        out.close();
    }

    static class InputReader {

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

        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);
                    if (len == -1) return -1;
                } catch (IOException e) {
                    e.fillInStackTrace();
                }
            return buff[next++];
        }

        int nextChar() {
            int c = getByte();
            while (c <= 32 || c == 127)
                if (-1 == (c = getByte())) return -1;
            return c;
        }

        int nextInt() {
            int n = 0, c = nextChar();
            boolean flag = true;
            if (c == '-') {
                c = getByte();
                flag = false;
            }
            while(c >= '0' && c <= '9') {
                n = n * 10 + (c & 0xf);
                c = getByte();
            }
            return flag? n: -n;
        }
    }
}

#5 版本分支

时间限制: 1.0s 内存限制: 256.0MB 本题满分: 71 分


问题描述

小明负责维护公司一个奇怪的项目。这个项目的代码一直在不断分支(branch)但是从未发生过合并(merge)。
现在这个项目的代码一共有N个版本,编号1~N,其中1号版本是最初的版本。
除了1号版本之外,其他版本的代码都恰好有一个直接的父版本;即这N个版本形成了一棵以1为根的树形结构。

如下图就是一个可能的版本树:

    1
   / \
  2   3
  |  / \
  5 4   6

现在小明需要经常检查版本x是不是版本y的祖先版本。你能帮助小明吗?


输入格式

第一行包含两个整数N和Q,代表版本总数和查询总数。
以下N-1行,每行包含2个整数u和v,代表版本u是版本v的直接父版本。
再之后Q行,每行包含2个整数x和y,代表询问版本x是不是版本y的祖先版本。


输出格式

对于每个询问,输出YES或NO代表x是否是y的祖先。


测试样例1

Input:
6 5
1 2
1 3
2 5
3 6
3 4
1 1
1 4
2 6
5 2
6 4

Output:
YES
YES
NO
NO
NO

评测用例规模与约定

对于30%的数据,1 <= N <= 1000 1 <= Q <= 1000
对于100%的数据,1 <= N <= 100000 1 <= Q <= 100000


code:

import java.io.InputStream;
import java.io.IOException;
import java.io.PrintWriter;

public class Main {
	
	static int[] link;
	
	public static void main(String[] args) throws IOException {
		InputReader in = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int N = in.nextInt(), Q = in.nextInt(), u, v, w;
		link = new int[N + 1];
		while (N-- > 1) {
			u = in.nextInt();
			v = in.nextInt();
			link[v] = u;
		}
		while (Q-- > 0) {
			v = in.nextInt();
			w = in.nextInt();
			if (v == 1)    w = 1;
			else while (w != 1) {
				if (w == v) break;
				w = link[w];
			}
			out.println(w == v? "YES": "NO");
		}
		out.close();
	}
	
	static class InputReader {
		
		InputStream in;
		int next, len;
		byte[] buff;
		
		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);
					if (len == -1) return -1;
				} catch (IOException e) {
					e.fillInStackTrace();
				}
			return buff[next++];
		}
		
		int nextChar() {
			int c = getByte();
			while (c <= 32 || c == 127)
				if (-1 == (c = getByte())) return -1;
			return c;
		}
		
		int nextInt() {
			int n = 0, c = nextChar();
			boolean flag = true;
			if (c == '-') {
				c = getByte();
				flag = false;
			}
			while(c >= '0' && c <= '9') {
				n = n * 10 + (c & 0xf);
				c = getByte();
			}
			return flag? n: -n;
		}
	}
}

用并查集硬搜吧,没有想到能在对数时间以内效验父节点内存不超限的数据结构


#6 防御力

时间限制: 1.0s 内存限制: 256.0MB 本题满分: 105 分


问题描述

小明最近在玩一款游戏。对游戏中的防御力很感兴趣。
我们认为直接影响防御的参数为“防御性能”,记作d,而面板上有两个防御值A和B,与d成对数关系,A=2d,B=3d(注意任何时候上式都成立)。
在游戏过程中,可能有一些道具把防御值A增加一个值,有另一些道具把防御值B增加一个值。
现在小明身上有n1个道具增加A的值和n2个道具增加B的值,增加量已知。

现在已知第i次使用的道具是增加A还是增加B的值,但具体使用那个道具是不确定的,请找到一个字典序最小的使用道具的方式,使得最终的防御性能最大。

初始时防御性能为0,即d=0,所以A=B=1。


输入格式

输入的第一行包含两个数n1,n2,空格分隔。
第二行n1个数,表示增加A值的那些道具的增加量。
第三行n2个数,表示增加B值的那些道具的增加量。
第四行一个长度为n1+n2的字符串,由0和1组成,表示道具的使用顺序。0表示使用增加A值的道具,1表示使用增加B值的道具。输入数据保证恰好有n1个0,n2个1。


输出格式

对于每组数据,输出n1+n2+1行,前n1+n2行按顺序输出道具的使用情况,若使用增加A值的道具,输出Ax,x为道具在该类道具中的编号(从1开始)。若使用增加B值的道具则输出Bx。最后一行输出一个大写字母E。


测试样例1

Input:
1 2
4
2 8
101

Output:
B2
A1
B1
E

Explanation:
操作过程如下:
操作  d         A              B
初始  0	        1              1
B2    2         4              9
A1    3	        8              27
B1   log3(29)   2^(log3(29))   29

可以证明,这个值是最大的。

测试样例2

Input:
3 0
7 11 13

000

Output:
A1
A2
A3
E

Explanation:
可见无论用什么顺序,A最后总为32,即d总为5,B总为243。 

评测用例规模与约定

对于20%的数据,字符串长度<=10000;
对于70%的数据,字符串长度<=200000;
对于100%的数据,字符串长度<=2000000,输入的每个增加值不超过230


code:

import java.io.InputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;

public class Main {

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int n1 = in.nextInt(), n2 = in.nextInt();
        Props[] A = new Props[n1];
        Props[] B = new Props[n2];
        for (int i = 0; i < n1; i++)
            A[i] = new Props(i, in.nextInt());
        for (int i = 0; i < n2; i++)
            B[i] = new Props(i, in.nextInt());
        Arrays.sort(A, new Comparator<Props>() {
            public int compare(Props arg0, Props arg1) {
                return arg0.val == arg1.val? (arg0.id - arg1.id) : (arg0.val - arg1.val);
            }
        });
        Arrays.sort(B, new Comparator<Props>() {
            public int compare(Props arg0, Props arg1) {
                return arg1.val == arg0.val? (arg0.id - arg1.id) : (arg1.val - arg0.val);
            }
        });
        Comparator idcmp = new Comparator<Props>() {
            public int compare(Props arg0, Props arg1) {
                return arg0.id - arg1.id;
            }
        };
        String line = in.nextLine().trim();
        for (int i = 0, a = 0, b = 0, c, high = line.length(); i < high;) {
            int start = i;
            int end = i + 1;
            char key = line.charAt(start);
            while(end < high && line.charAt(end) == key) end++;
            i = end;
            if (end - start == 1) {
                if (key == '0') out.println("A" + (A[a++].id + 1));
                else			out.println("B" + (B[b++].id + 1));
            } else {
                if (key == '0') {
                    Arrays.sort(A, a, c = a + end - start, idcmp);
                    while (a < c)
                        out.println("A" + (A[a++].id + 1));
                } else {
                    Arrays.sort(B, b, c = b + end - start, idcmp);
                    while (b < c)
                        out.println("B" + (B[b++].id + 1));
                }
            }
        }
        out.println("E");
        out.close();
    }

    static class Props {

        int id, val;

        Props(int id, int val) {
            this.id = id;
            this.val = val;
        }
    }

    static class InputReader {

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

        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);
                    if (len == -1) return -1;
                } catch (IOException e) {
                    e.fillInStackTrace();
                }
            return buff[next++];
        }

        int getChar() {
            int c = getByte();
            while (c != -1 && (c <= 32 || c == 127)) c = getByte();
            return c;
        }

        int nextInt() {
            int n = 0, c = getChar();
            boolean flag = true;
            if (c == '-') {
                c = getByte();
                flag = false;
            }
            while (c >= '0' && c <= '9') {
                n = n * 10 + (c & 0xf);
                c = getByte();
            }
            return flag? n: -n;
        }

        String nextLine() {
            StringBuilder res = new StringBuilder();
            int c = getChar();
            while (c != '\n') {
                res.appendCodePoint(c);
                c = getByte();
            }
            return res.toString();
        }
    }
}

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值