蓝桥杯 Java B组省赛真题【2018】

题目来源:一叶之修


1. 第几天(结果填空 - 5分)

第几天

2000年的1月1日,是那一年的第1天。
那么,2000年的5月4日,是那一年的第几天?

注意:需要提交的是一个整数,不要填写任何多余内容。

31+29+31+30+4 = 125

答案:125


2. 方格计数(结果填空 - 7分)

方格计数

如图所示,在二维平面上有无数个1x1的小方格。
在这里插入图片描述
我们以某个小方格的一个顶点为圆心画一个半径为1000的圆。
你能计算出这个圆里有多少个完整的小方格吗?

注意:需要提交的是一个整数,不要填写任何多余内容。

/*
	1. 以圆心为(0,0)点,半径为1000。
	2. 只需计算1/4个圆就可以。
	3. (1000,1000)坐标内,计算满足圆心到点距离小于半径的点
	4. x坐标和y坐标上的点不算在内,也就是从 1开始循环
*/
public class Main {
	static int[] a = {1,2,3,4,5,6,7,8,9};
	static int ans = 0;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int d=1000;
		int ans=0;
		
		for(int i=1; i<=1000; i++) {
			for(int j=1; j<=1000; j++) {
				if(i*i+j*j<=d*d)
					ans++;
			}
		}
		System.out.println(ans*4);
	}
}

答案:3137548



3. 复数幂(结果填空 - 13分)

复数幂

设i为虚数单位。对于任意正整数n,(2+3i)^n 的实部和虚部都是整数。

求 (2+3i)^123456 等于多少?即(2+3i)的123456次幂,这个数字很大,要求精确表示。

答案写成 "实部±虚部i"的形式,实部和虚部都是整数(不能用科学计数法表示),中间任何地方都不加空格,实部为正时前面不加正号。(2+3i)^2 写成: -5+12i,
(2+3i)^5 的写成: 122-597i

注意:需要提交的是一个很庞大的复数,不要填写任何多余内容。

/*
	1. (a+bi)*(c+di) = a*c+a*di+c*bi-b*d = (a*c-b*d)+(a*d+c*b)i
	2. 使用BigInteger大整型数
	3. 加法 a.add(b) / 减法 a.subtract(b) / 乘法 a.multiply(b)
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	static int N = 123456;
	public static void main(String[] args) throws FileNotFoundException {
		Scanner sc = new Scanner(System.in);
		BigInteger a = new BigInteger("2");
		BigInteger b = new BigInteger("3");
		BigInteger c = new BigInteger("2");
		BigInteger d = new BigInteger("3");
		
		for(int i=1; i<N; i++) {
			BigInteger A = a.multiply(c).subtract(b.multiply(d));
			BigInteger B = a.multiply(d).add(c.multiply(b));
			a = A;
			b = B;
		}
		PrintStream out = System.out;
		PrintStream ps = new PrintStream(new File("ans.txt"));//默认在项目的路径
		System.setOut(ps);//输出在ans.txt里
		System.out.println(a.toString()+b.toString()+"i");
	}
}

答案太长了,自己用代码跑一下吧


4. 测试次数(结果填空 - 17分)

x星球的居民脾气不太好,但好在他们生气的时候唯一的异常举动是:摔手机。
各大厂商也就纷纷推出各种耐摔型手机。x星球的质监局规定了手机必须经过耐摔测试,并且评定出一个耐摔指数来,之后才允许上市流通。

x星球有很多高耸入云的高塔,刚好可以用来做耐摔测试。塔的每一层高度都是一样的,与地球上稍有不同的是,他们的第一层不是地面,而是相当于我们的2楼。

如果手机从第7层扔下去没摔坏,但第8层摔坏了,则手机耐摔指数=7。
特别地,如果手机从第1层扔下去就坏了,则耐摔指数=0。
如果到了塔的最高层第n层扔没摔坏,则耐摔指数=n

为了减少测试次数,从每个厂家抽样3部手机参加测试。

某次测试的塔高为1000层,如果我们总是采用最佳策略,在最坏的运气下最多需要测试多少
次才能确定手机的耐摔指数呢?

请填写这个最多测试次数。

注意:需要填写的是一个整数,不要填写任何多余内容。

leetcode 1884.鸡蛋掉落与此题类似。 【这里有一个:超级清晰的题解

/*
	1. 动态规划, 定义dp(n,k)表示n层楼k个手机要测试几次
	2. 初状态:dp[0][k]=0(0层楼不需要测),dp[n][1]=n(只有一个手机只能从1楼开始扔,有几层就得扔几次)
	3. 状态转移:dp[n][k] = Math.min(res,Math.max(dp[n-i][k],dp[i-1][k-1])+1)
	4. 从i层扔手机会有两种情况:
	   1)坏了,那接下来的测试就变成了i-1层k-1个手机。
	   2)没坏,就是n-i层k个手机
	   两种情况怎么选?根据题目要选运气坏的情况,所以测试次数 = Math.max(dp[n-i][k],dp[i-1][k-1])+1
	   【注意并不是手机坏了就是运气不好,而是哪种情况导致接下来要测的次数更多】
	5. i怎么选?
	   遍历(1,n),从结果中选择测试数量少的。
	   【我们总是采用最佳策略,即每次选择的楼层都是会使我们最后总测试次数更少的】
	   res = Math.min(res,Math.max(dp[n-i][k],dp[i-1][k-1])+1)
	   遍历后得到一个最小的res,res赋给dp[n][k]
*/
import java.util.Scanner;

public class Main {	
	static int n = 100;
	static int k = 2;
	static int[][] memo;
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		memo = new int[n+1][k+1];
		System.out.println(dp(n,k));
	}
	
	static int dp(int n, int k) {
		
		if(n == 0) return 0;
		if(k == 1) return n;
		
		if(memo[n][k] != 0) {
			return memo[n][k];
		}

		int res = Integer.MAX_VALUE;
		for(int i=1; i<=n; i++) {
			res = Math.min(res, Math.max(dp(n-i,k), dp(i-1,k-1))+1);
		}
		memo[n][k] = res;
		return res;
	}
}

答案:19


5. 快速排序(代码填空 - 9分)

快速排序

以下代码可以从数组a[]中找出第k小的元素。
它使用了类似快速排序中的分治算法,期望时间复杂度是O(N)的。

请仔细阅读分析源码,填写划线部分缺失的内容。
注意:只提交划线部分缺少的代码,不要抄写任何已经存在的代码或符号。

import java.util.Random;
public class Main{
	public static int quickSelect(int a[], int l, int r, int k) {
		Random rand = new Random();
		int p = rand.nextInt(r - l + 1) + l;
		int x = a[p];
		int tmp = a[p]; a[p] = a[r]; a[r] = tmp;
		int i = l, j = r;
		while(i < j) {
            while(i < j && a[i] < x) i++;
            if(i < j) {
                    a[j] = a[i];
                    j--;
            }
            while(i < j && a[j] > x) j--;
            if(i < j) {
                    a[i] = a[j];
                    i++;
            }
        }
        a[i] = x;
        p = i;
        if(i - l + 1 == k) return a[i];
        if(i - l + 1 < k) return quickSelect( _________________________________ ); //填空
        else return quickSelect(a, l, i - 1, k);
	}
	public static void main(String args[]) {
		int [] a = {1, 4, 2, 8, 5, 7};
		System.out.println(quickSelect(a, 0, 5, 4));
	}
}

答案:quickSelect(a, i+1, r, k-i+l-1)



6. 递增三元组(程序设计 - 11分)

递增三元组

给定三个整数数组
A = [A1, A2, … AN],
B = [B1, B2, … BN],
C = [C1, C2, … CN],
请你统计有多少个三元组(i, j, k) 满足:

1. 1 <= i, j, k <= N
2. Ai < Bj < Ck

--------------------
【输入格式】
第一行包含一个整数N。
第二行包含N个整数A1, A2, … AN。
第三行包含N个整数B1, B2, … BN。
第四行包含N个整数C1, C2, … CN。
【输出格式】
一个整数表示答案

【输入样例】
3
1 1 1
2 2 2
3 3 3
【输出样例】
27

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

对于30%的数据,1 <= N <= 100
对于60%的数据,1 <= N <= 1000
对于100%的数据,1 <= N <= 100000 0 <= Ai, Bi, Ci <= 100000

/*
	1. 暴力解法。三个for,时间肯定会超时。
	2. 优化 1:最后一个循环用二分法
	3. 优化 2:b[]中查找比a[i]大的数,c[]中查找比b[i]大的数,所有这两个数相乘再累加就是答案。
*/
// 暴力法
import java.util.Scanner;

public class Main {	
	static int n = 1000;
	static int k = 3;
	static int[][] memo;
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 输入数据
		int n = sc.nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		int[] c = new int[n];
		int ans = 0;
		for(int i=0; i<n; i++) {
			a[i] = sc.nextInt();
		}
		for(int i=0; i<n; i++) {
			b[i] = sc.nextInt();
		}
		for(int i=0; i<n; i++) {
			c[i] = sc.nextInt();
		}
		
		for(int i=0; i<n; i++) {
			for(int j=0; j<n; j++) {
				for(int k=0; k<n; k++) {
					if(a[i] < b[j] && b[j] < c[k]) ans = ans+1;
				}
			}
		}
		System.out.println(ans);
	}
}

// 优化 1
import java.util.Scanner;
import java.util.*;

public class Main {	
	static int n = 1000;
	static int k = 3;
	static int[][] memo;
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 输入数据
		int n = sc.nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		int[] c = new int[n];
		int ans = 0;
		for(int i=0; i<n; i++) {
			a[i] = sc.nextInt();
		}
		for(int i=0; i<n; i++) {
			b[i] = sc.nextInt();
		}
		for(int i=0; i<n; i++) {
			c[i] = sc.nextInt();
		}
		
		Arrays.sort(a);
		Arrays.sort(b);
		Arrays.sort(c);
		
		
		for(int i=0; i<n; i++) {
			for(int j=0; j<n; j++) {
//				for(int k=0; k<n; k++) {
//					if(a[i] < b[j] && b[j] < c[k]) ans = ans+1;
//				}
				// 第三重循环用二分法查找
				if(a[i] < b[j]) {
					int l=0,r=n-1;
					while(l<=r) {
						int m = (l+r)/2;
			            if(c[m]>b[j]) r = m - 1;
			            else    l = m + 1;
					}
					// l为排序后的数组c大于b[j]值的开始边界,
					// 即数组c[l,n-1]中的数都满足条件
					ans = ans + (n-l);
				}
			}
		}
		System.out.println(ans);
	}
}


7. 螺旋折线(程序设计- 19分)

如图所示的螺旋折线经过平面上所有整点恰好一次。
对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度。
在这里插入图片描述

例如dis(0, 1)=3, dis(-2, -1)=9

给出整点坐标(X, Y),你能计算出dis(X, Y)吗?


--------------------
【输入格式】
X和Y
【输出格式】
输出dis(X, Y)

【输入样例】
0 1
【输出样例】
3

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

对于40%的数据,-1000 <= X, Y <= 1000
对于70%的数据,-100000 <= X, Y <= 100000
对于100%的数据, -1000000000 <= X, Y <= 1000000000

/*
	1. 将螺旋折线看作套起来的一个个正方形。最里面的是边长为2的正方形。
	2. 正方形边长依次为 1*2、2*2、3*2、4*2 ... n*2
	3. 判断点所在的正方形 Math.max(Math.abs(x),Math.abs(y))
	4. 所在的正方形内所有正方形的边长之和。
	   C = (1、2、3、4 ... n-1)*2*4 = (n*(n-1))/2 *2*4 = 4*n*(n-1)
	5. 判断最外层正方形所在位置
	   x = -n  d=n+y
	   y = n   d=2n+n+x
	   x = n   d=4n+n-y
	   y = -n  d=6n+n-x
*/
import java.util.Scanner;
public class Main {	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 输入数据
		int x = sc.nextInt();
		int y = sc.nextInt();
		// 最大正方形的边长
		int n = Math.max(Math.abs(x), Math.abs(y));
		// 内部正方形边长和
		int ans = 4*n*(n-1);
		// 分情况计算所在点
		if( x==-n && y==-n) ans += 8*n;
		else if(x==-n) ans += n+y;
		else if(y==n) ans += 3*n+x;
		else if(x==n) ans += 5*n-y;
		else if(y==-n) ans += 7*n-x;
		
		System.out.println(ans);
	}
}


8. 日志统计(程序设计 - 21分)

小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有N行。其中每一行的格式是:
ts id
表示在ts时刻编号id的帖子收到一个"赞"。

现在小明想统计有哪些帖子曾经是"热帖"。如果一个帖子曾在任意一个长度为D的时间段内收到不少于K个赞,小明就认为这个帖子曾是"热帖"。

具体来说,如果存在某个时刻T满足该帖在[T, T+D)这段时间内(注意是左闭右开区间)收到不少于K个赞,该帖就曾是"热帖"。

给定日志,请你帮助小明统计出所有曾是"热帖"的帖子编号。

--------------------
【输入格式】
第一行包含三个整数N、D和K。
以下N行每行一条日志,包含两个整数ts和id。
【输出格式】
按从小到大的顺序输出热帖id。每个id一行。

【输入样例】
7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3
【输出样例】
1
3

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

对于50%的数据,1 <= K <= N <= 1000
对于100%的数据,1 <= K <= N <= 100000 0 <= ts <= 100000 0 <= id <= 100000

/*
	1. 按ts排序,尺取法来判断长度为D的区间内,各id点赞次数
	2. ts排序需与id绑定进行,否则无法定位原id。需自定义对象{ts,id},重写排序方法。
	3. record 来存储输入的ts,id数据。ArrayList<Node> record
	4. cnt 统计对应id点赞次数。map<Integer, Integer>,判断点赞次数是否符合来记录对应id。
	5. ans 统计符合条件的id。TreeSet类型可自动排序。
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeSet;
public class Main {	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 输入
		int n = sc.nextInt();
		int d = sc.nextInt();
		int k = sc.nextInt();
		
		ArrayList<Node> record = new ArrayList<>();
		TreeSet<Integer> ans = new TreeSet<>();
		HashMap<Integer, Integer> cnt = new HashMap<>();

		// record 存储读入的数据
		for(int i=0; i<n; i++) {
			int ts = sc.nextInt();
			int id = sc.nextInt();
			record.add(new Node(ts,id));
		}
		// 根据对象的ts进行排序
		Collections.sort(record);
		
		// 尺取法,双指针(i, j)
		int j = 0;
		for(int i=0; i<n; i++) {
			// j<n 且 i-j在[T,T+D)区间内,cnt统计此时id值和点赞数
			while(j<n && record.get(j).ts - record.get(i).ts < d) {
				// 满足条件对应id点赞数+1
				cnt.put(record.get(j).id, (cnt.get(record.get(j).id)==null?0:cnt.get(record.get(j).id))+1);
				// 点赞数>=k,记录id
				if(cnt.get(record.get(j).id) >= k) {
					ans.add(record.get(j).id);
				}
				j++;
			}
			// 在i++后,i就不在时间d内了。
			// 所以record.get(i).id的点赞数就不作数了要减掉。
			cnt.put(record.get(i).id, cnt.get(record.get(i).id)-1);
		}
		// 输出结果
		for(int x:ans) {
			System.out.println(x);
		}
	}
	
	// 要指定比较的规则,需要在自定义类中实现Comparable接口,并重写接口中的compareTo方法
	static class Node implements Comparable<Node>{
		int ts,id;
		public Node(int ts, int id) {
			this.ts = ts;
			this.id = id;
		}
		//@Override
		public int compareTo(Node o) {
			return this.ts - o.ts;
		}
	}
}

自定义类实现Comparable接口
数据结构
ArrayList<Node> record
record.add(x)添加元素,.get(i)获取元素,.remove(i)删除元素,.size()计算元素数量,for(x: record)迭代
Set<Integer> ans
ans.add(x)添加元素,.get(i)获取元素,.remove(i)删除元素,.size()计算元素数量,for(x: ans)迭代
HashMap<Integer, Integer> cnt
cnt.put(i,x)添加键值对,.get(i)得到x,.remove(i)删除元素,.size()计算元素数量,for(i: cnt.keySet())迭代键,for(x: cnt.values())迭代值



9. 全球变暖(程序设计 - 23分)

你有一张某海域NxN像素的照片,".“表示海洋、”#"表示陆地,如下所示:

.......
.##....
.##....
....##.
..####.
...###.
.......

其中"上下左右"四个方向上连在一起的一片陆地组成一座岛屿。例如上图就有2座岛屿。

由于全球变暖导致了海面上升,科学家预测未来几十年,岛屿边缘一个像素的范围会被海水淹没。具体来说如果一块陆地像素与海洋相邻(上下左右四个相邻像素中有海洋),它就会被淹没。

例如上图中的海域未来会变成如下样子:

.......
.......
.......
.......
....#..
.......
.......

请你计算:依照科学家的预测,照片中有多少岛屿会被完全淹没。

--------------------
【输入格式】
第一行包含一个整数N。 (1 <= N <= 1000)
以下N行N列代表一张海域照片。
照片保证第1行、第1列、第N行、第N列的像素都是海洋。
【输出格式】
一个整数表示答案。

【输入样例】
7

.......
.##....
.##....
....##.
..####.
...###.
.......  

【输出样例】
1

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

/*
	1. 求会被完全淹没,即连通块的数量和与海相邻的块的数量一样多
	2. cnt1记录连通块数量,cnt2记录邻海块数量。相同 ans++
	3. 用dfs递归太深,内存可能会超,用bfs来搜索连通块。
	4. 队列实现bfs
*/
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {	
	static int n;
	static char[][] data;	// 存储数据
	static int[][] mark;	//用来标记已访问元素
	static int ans = 0;
	// 四个方向
	static int[] dx = {1, 0, 0, -1};
    static int[] dy = {0, 1, -1, 0};
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 输入
		n = sc.nextInt();
		data = new char[n][n];
		mark = new int[n][n];
		// 存储数据
		for(int i=0; i<n; i++) {
			data[i] = sc.next().toCharArray();
		}
		// 双循环搜索'#',从'#'开始宽度优先搜索
		for(int i=0; i<n; i++) {
			for(int j=0; j<n; j++) {
				// 未访问过 && 是'#'
				if(mark[i][j] == 0 && data[i][j] == '#') {
					bfs(i,j);
				}
			}
		}
		
		System.out.println(ans);
		
	}
	static void bfs(int i, int j) {
		int cnt1=0,cnt2=0;
		mark[i][j] = 1;	// 标记已访问
		// 队列中存一个数组存放坐标
		Queue<int[]> queue = new LinkedList<int[]>();
		queue.offer(new int[] {i,j});
		
		while(!queue.isEmpty()) {
			int[] cell = queue.poll();
			cnt1++;		// 连通块数量++
			boolean swed = false;	//(i,j)是否会被淹没
			// 四个方向遍历
			for(int k=0; k<4; k++) {
				int x = cell[0] + dx[k], y = cell[1] + dy[k];
				// 满足条件(不越界&&为'.')会被淹没
				if(0<=x && x<n && 0<=y && y<n && data[x][y]=='.') swed = true;
				// 满足条件(不越界&&为'#'&&未被访问)加入队列
				if(0<=x && x<n && 0<=y && y<n && data[x][y]=='#' && mark[x][y]==0) {
					queue.offer(new int[] {x,y});
					mark[x][y] = 1;
				}
			}
			// 四个方向遍历完后只要有一个是领海的,swed就会被标为true
			if(swed) cnt2++;
		}
		
		// while循环结束表示访问完了一个连通块
		// 连通块数量记录在cnt1,被淹没数量记录在cnt2
		if(cnt1 == cnt2) ans++;
	}
	
}


10. 堆的计数(程序设计 - 25分)

我们知道包含N个元素的堆可以看成是一棵包含N个节点的完全二叉树。
每个节点有一个权值。对于小根堆来说,父节点的权值一定小于其子节点的权值。

假设N个节点的权值分别是1~N,你能求出一共有多少种不同的小根堆吗?

例如对于N=4有如下3种:

    1
   / \
  2   3
 /
4

    1
   / \
  3   2
 /
4

    1
   / \
  2   4
 /
3

由于数量可能超过整型范围,你只需要输出结果除以1000000009的余数。

--------------------
【输入格式】
一个整数N。
【输出格式】
一个整数表示答案。

【输入样例】
4
【输出样例】
3

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

对于40%的数据,1 <= N <= 1000
对于70%的数据,1 <= N <= 10000
对于100%的数据,1 <= N <= 100000

/*
	
*/
public class Main(){
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		n = in.nextInt();
		f = new long[n+5];
		inv = new long[n+5];
		f[0]=1;
		for(int i=1;i<n+5;i++) {
			f[i]=f[i-1]*i%mod;
			inv[i] = mpow(f[i],mod-2);
		}
		
		
	}
	
	static int n;
	static long mod = 1000000009;
	static long[] f;
	static long[] inv;
	
	static long C(int n,int m) {
		return f[n]*inv[m]%mod*inv[n-m]%mod;
	}
	
	static long mpow(long a,long n) {//快速幂
		if(n==0 || a==1)
			return 1;
		long ans=1;
		while(n!=0) {
			if(n%2==1)
				ans = a*ans%mod;
			a=a*a%mod;
			n>>=1;
		}
		return ans;
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值