1、本题为javaB组的模拟题
2、本文全部为java代码进行答题
3、【】 中间是填空题的答案
一、填空题
最小数
最小数
问题描述:请找到一个大于 2022 的最小数,这个数转换成十六进制之后,所有的数位(不含前导 0)都为字母(A 到 F)。
请将这个数的十进制形式作为答案提交。
这里直接使用Integer的方法toHexString(),转为十六进制 【2730】
public class Main{
public static void main(String[] args) {
for (int j = 2022; j <= 100000; j++) {
String str = Integer.toHexString(j);
System.out.println(str.toUpperCase() + " " + j);// AAA 2730
}
}
}
Excel的列
问题描述:在 Excel 中,列的名称使用英文字母的组合。前 26 列用一个字母,依次为 A 到 Z,接下来 26*26 列使用两个字母的组合,依次为 AA 到 ZZ。
请问第 2022 列的名称是什么?
本题两个字母的情况下,2022超出范围,所以需要三个字母【BYT】
public class Main{
public static void main(String[] args) {
int count = 26 + 26 * 26;
for (int i = 'A'; i <= 'Z'; i++) {
for (int j = 'A'; j <= 'Z'; j++) {
for (int k = 'A'; k <= 'Z'; k++) {
count++;
if (count == 2022) {
System.out.println((char) i + "" + (char) j + "" + (char) k);
}
}
}
}
}
}
日期数
问题描述:对于一个日期,我们可以计算出年份的各个数位上的数字之和,也可以分别计算月和日的各位数字之和。请问从 1900 年 1 月 1 日至 9999 年 12 月 31 日,总共有多少天,年份的数位数字之和等于月的数位数字之和加日的数位数字之和。
例如,2022年11月13日满足要求,因为 2+0+2+2=(1+1)+(1+3) 。
请提交满足条件的日期的总数量。
本题也是常见的日期类题目,就固定模式来写就行 【70910】
public class Main{
public static void main(String[] args) {
int[] mouthDays = {0,31,28,31,30,31,30,31,31,30,31,30,31};// 每月的时间
int mouth = 1;
int ans = 0;
int year = 1900;
while(true) {// 逐年递增进行验证
int y1 = year/1000;// 千位
int y2 = year%1000/100;// 百位
int y3 = year%1000%100/10;// 十位
int y4 = year%1000%100%10;// 各位
int ySum = y1 + y2 + y3 + y4;
if (year%4==0 && year%100!=0 || year%400==0) {
mouthDays[2] = 29;
}else {
mouthDays[2] = 28;
}
for(int i = 1;i <= mouthDays[mouth];i++) {
int m1 = mouth/10;
int m2 = mouth%10;
int d1 = i / 10;
int d2 = i % 10;
if (ySum == m1 + m2 + d1 + d2) {
ans ++;
}
}
mouth++;
if (mouth > 12) {
year++;
mouth = 1;
}
if (year > 9999) {
break;
}
}
System.out.println(ans);
}
}
取数
问题描述:小蓝有 30 个数,分别为:99, 22, 51, 63, 72, 61, 20, 88, 40, 21, 63, 30, 11, 18, 99, 12, 93, 16, 7, 53, 64, 9, 28, 84, 34, 96, 52, 82, 51, 77 。
小蓝可以在这些数中取出两个序号不同的数,共有 30*29/2=435 种取法。
请问这 435 种取法中,有多少种取法取出的两个数的乘积大于等于 2022 。
不重复抽取,就像数学知识中的概率的学习一样,做不放回抽取且都是当前数的后面的数。【189】
public class Main{
public static void main(String[] args) {
int[] nums = {99,22,51,63,72,61,20,88,40,21,63,30,11,18,99,12,93,16,7,53,64,9,28,84,34,96,52,82,51,77};
int cnt = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] * nums[j] >= 2022) {
cnt++;
}
}
}
System.out.println(cnt);
}
}
最大连通块
问题描述:小蓝有一个 30 行 60 列的数字矩阵,矩阵中的每个数都是 0 或 1 。
110010000011111110101001001001101010111011011011101001111110
010000000001010001101100000010010110001111100010101100011110
001011101000100011111111111010000010010101010111001000010100
101100001101011101101011011001000110111111010000000110110000
010101100100010000111000100111100110001110111101010011001011
010011011010011110111101111001001001010111110001101000100011
101001011000110100001101011000000110110110100100110111101011
101111000000101000111001100010110000100110001001000101011001
001110111010001011110000001111100001010101001110011010101110
001010101000110001011111001010111111100110000011011111101010
011111100011001110100101001011110011000101011000100111001011
011010001101011110011011111010111110010100101000110111010110
001110000111100100101110001011101010001100010111110111011011
111100001000001100010110101100111001001111100100110000001101
001110010000000111011110000011000010101000111000000110101101
100100011101011111001101001010011111110010111101000010000111
110010100110101100001101111101010011000110101100000110001010
110101101100001110000100010001001010100010110100100001000011
100100000100001101010101001101000101101000000101111110001010
101101011010101000111110110000110100000010011111111100110010
101111000100000100011000010001011111001010010001010110001010
001010001110101010000100010011101001010101101101010111100101
001111110000101100010111111100000100101010000001011101100001
101011110010000010010110000100001010011111100011011000110010
011110010100011101100101111101000001011100001011010001110011
000101000101000010010010110111000010101111001101100110011100
100011100110011111000110011001111100001110110111001001000111
111011000110001000110111011001011110010010010110101000011111
011110011110110110011011001011010000100100101010110000010011
010011110011100101010101111010001001001111101111101110011101
如果从一个标为 1 的位置可以通过上下左右走到另一个标为 1 的位置,则称两个位置连通。与某一个标为 1 的位置连通的所有位置(包括自己)组成一个连通分块。
请问矩阵中最大的连通分块有多大?
本题是典型的搜索
问题,有上下左右四个方向的操作,对二维数组中是否为1
进行判断【148】
public class Main{
public static void main(String[] args) {
String string = "110010000011111110101001001001101010111011011011101001111110"
+ "010000000001010001101100000010010110001111100010101100011110"
+ "001011101000100011111111111010000010010101010111001000010100"
+ "101100001101011101101011011001000110111111010000000110110000"
+ "010101100100010000111000100111100110001110111101010011001011"
+ "010011011010011110111101111001001001010111110001101000100011"
+ "101001011000110100001101011000000110110110100100110111101011"
+ "101111000000101000111001100010110000100110001001000101011001"
+ "001110111010001011110000001111100001010101001110011010101110"
+ "001010101000110001011111001010111111100110000011011111101010"
+ "011111100011001110100101001011110011000101011000100111001011"
+ "011010001101011110011011111010111110010100101000110111010110"
+ "001110000111100100101110001011101010001100010111110111011011"
+ "111100001000001100010110101100111001001111100100110000001101"
+ "001110010000000111011110000011000010101000111000000110101101"
+ "100100011101011111001101001010011111110010111101000010000111"
+ "110010100110101100001101111101010011000110101100000110001010"
+ "110101101100001110000100010001001010100010110100100001000011"
+ "100100000100001101010101001101000101101000000101111110001010"
+ "101101011010101000111110110000110100000010011111111100110010"
+ "101111000100000100011000010001011111001010010001010110001010"
+ "001010001110101010000100010011101001010101101101010111100101"
+ "001111110000101100010111111100000100101010000001011101100001"
+ "101011110010000010010110000100001010011111100011011000110010"
+ "011110010100011101100101111101000001011100001011010001110011"
+ "000101000101000010010010110111000010101111001101100110011100"
+ "100011100110011111000110011001111100001110110111001001000111"
+ "111011000110001000110111011001011110010010010110101000011111"
+ "011110011110110110011011001011010000100100101010110000010011"
+ "010011110011100101010101111010001001001111101111101110011101";
char[][] chars = new char[35][65];
boolean[][] check = new boolean[35][65];
for (int i = 0; i < 30; i++) {
chars[i] = string.substring(i*60, i*60 + 60).toCharArray();
}
int res = 0;
for(int i = 0; i < 30; i++){
for(int j = 0; j < 60; j++){
if(chars[i][j] == '1' && check [i][j] == false){
cnt = 1;
dfs(chars, check, i, j);
res = Math.max(res, cnt);
}
}
}
System.out.println(res);
}
public static int[] getX = {0, 1, 0, -1};
public static int[] getY = {1, 0, -1, 0};
public static int cnt = 0;
public static void dfs(char[][] chars, boolean[][] check, int x, int y){
check[x][y] = true;
for(int i = 0; i < 4; i++){
int newx = x + getX[i];
int newy = y + getY[i];
// 对上下左右分别进行,只是简化了下代码,优化了分支语句
if(newx >= 0 && newx < 30 && newy >= 0 && newy < 60 && a[newx][newy] == '1' && st[newx][newy] == false){
cnt++;
dfs(chars, check, newx, newy);
}
}
}
}
二、编程题
一周第几天
问题描述:给定一天是一周中的哪天,请问 n 天后是一周中的哪天?
输入格式
输入第一行包含一个整数 w,表示给定的天是一周中的哪天,w 为 1 到 6 分别表示周一到周六,w 为 7 表示周日。
第二行包含一个整数 n。
输出格式
输出一行包含一个整数,表示 n 天后是一周中的哪天,1 到 6 分别表示周一到周六,7 表示周日。
----------------------------
样例输入
6
10
样例输出
2
评测用例规模与约定
对于所有评测用例,1 <= n <= 1000000。
本题对于输入的n,如果是7的倍数,则直接输出w
如果不是7的倍数,那么需要处理,详细见代码。
import java.io.*;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int w = scanner.nextInt();
int n = scanner.nextInt();
int num = 0;
if (n % 7 == 0) {// 间隔天数为7的倍数
num = w;
}else {
num = w + (n % 7);
if (num > 7) {
num = num - 7;
}
}
System.out.println(num);
}
}
被覆盖的点
问题描述:小蓝负责一块区域的信号塔安装,整块区域是一个长方形区域,建立坐标轴后,西南角坐标为 (0, 0), 东南角坐标为 (W, 0), 西北角坐标为 (0, H), 东北角坐标为 (W, H)。其中 W, H 都是整数。
他在 n 个位置设置了信号塔,每个信号塔可以覆盖以自己为圆心,半径为 R 的圆形(包括边缘)。
为了对信号覆盖的情况进行检查,小蓝打算在区域内的所有横纵坐标为整数的点进行测试,检查信号状态。其中横坐标范围为 0 到 W,纵坐标范围为 0 到 H,总共测试 (W+1) * (H+1) 个点。
给定信号塔的位置,请问这 (W+1)*(H+1) 个点中有多少个点被信号覆盖。
输入格式
输入第一行包含四个整数 W, H, n, R,相邻整数之间使用一个空格分隔。
接下来 n 行,每行包含两个整数 x, y,表示一个信号塔的坐标。信号塔可能重合,表示两个信号发射器装在了同一个位置。
输出格式
输出一行包含一个整数,表示答案。
样例输入
10 10 2 5
0 0
7 0
样例输出
57
评测用例规模与约定
对于所有评测用例,1 <= W, H <= 100,1 <= n <= 100, 1 <= R <= 100, 0 <= x <= W, 0 <= y <= H。
思路:本题对于 (W+1)(H+1) 个点中有多少个点被信号覆盖,需要用到数学公式(x - i) * (x - i) + (y - j) * (y - j) <= r * r
来判断是否在覆盖范围之内,枚举(W+1)(H+1) 个点【暂时想到的就是这样了
】,对输入的信号塔坐标,进行上式计算,满足条件计数+1,同时对其进行Math.max(),比较找出最大值。但是考虑到n个坐标中可能有重复,所以需要进行记录,保证唯一。
import java.util.Scanner;
public class Main {
static int w, h, n, r;
static int[] X = new int[110];
static int[] Y = new int[110];
static boolean check(int x, int y)
{
for (int i = 0; i < n; ++ i )
{
int tx = x - X[i], ty = y - Y[i];
tx *= tx;
ty *= ty;
if (tx + ty <= r * r)
return true;
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
w = sc.nextInt();
h = sc.nextInt();
n = sc.nextInt();
r = sc.nextInt();
for (int i = 0; i < n; ++ i ) {
X[i] = sc.nextInt();
Y[i] = sc.nextInt();
}
int res = 0;
for (int i = 0; i <= w; ++ i )
for (int j = 0; j <= h; ++ j )
if (check(i, j))
res ++;
System.out.println(res);
}
}
未被清理的区域
问题描述:小蓝有一个 n * m 大小的矩形水域,小蓝将这个水域划分为 n 行 m 列,行数从 1 到 n 标号,列数从 1 到 m 标号。每行和每列的宽度都是单位 1 。
现在,这个水域长满了水草,小蓝要清理水草。
每次,小蓝可以清理一块矩形的区域,从第 r1 行(含)到第 r2 行(含)的第 c1 列(含)到 c2 列(含)。
经过一段时间清理后,请问还有多少地方没有被清理过。
输入格式
输入第一行包含两个整数 n, m,用一个空格分隔。
第二行包含一个整数 t ,表示清理的次数。
接下来 t 行,每行四个整数 r1, c1, r2, c2,相邻整数之间用一个空格分隔,表示一次清理。请注意输入的顺序。
输出格式
输出一行包含一个整数,表示没有被清理过的面积。
样例输入
2 3
2
1 1 1 3
1 2 2 2
样例输出
2
样例输入
30 20
2
5 5 10 15
6 7 15 9
样例输出
519
评测用例规模与约定
对于所有评测用例,1 <= r1 <= r2 <= n <= 100, 1 <= c1 <= c2 <= m <= 100, 0 <= t <= 100。
思路:本题可以暴力或者使用二位差分【追求满分,将超时控制到最小可能性发生】
暴力
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int n = scanner.nextInt();// n行
int m = scanner.nextInt();// m列
int t = scanner.nextInt();// t次
int[][] nums = new int[n + 1][m + 1];
while (t-- > 0) {
int r1 = scanner.nextInt();
int c1 = scanner.nextInt();
int r2 = scanner.nextInt();
int c2 = scanner.nextInt();
for(int i = r1;i <= r2;i++) {
for(int j = c1;j <= c2;j++) {
nums[i][j] = 1;
}
}
}
int ans = 0;
for(int i = 1;i <= n;i++) {
for(int j = 1;j <= m;j++) {
if (nums[i][j] == 0) {
ans ++;
}
}
}
System.out.println(ans);
}
}
二维差分,存一个差分矩阵,最后再使用一遍前缀和即可得答案
差分微讲解 简单了解 JAVA
差分详解 非常详细 C++
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int t = scanner.nextInt();
int[][] a = new int[n + 10][m + 10];
while(t-- > 0){
int r1 = scanner.nextInt();
int c1 = scanner.nextInt();
int r2 = scanner.nextInt();
int c2 = scanner.nextInt();
a[r1][c1]++;
a[r2 + 1][c1]--;
a[r1][c2 + 1]--;
a[r2 + 1][c2 + 1]++;
}
int res = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
if(a[i][j] == 0) res++;
}
}
System.out.println(res);
}
}
滑行距离
问题描述:小蓝准备在一个空旷的场地里面滑行,这个场地的高度不一,小蓝用一个 n 行 m 列的矩阵来表示场地,矩阵中的数值表示场地的高度。
如果小蓝在某个位置,而他上、下、左、右中有一个位置的高度(严格)低于当前的高度,小蓝就可以滑过去,滑动距离为 1 。
如果小蓝在某个位置,而他上、下、左、右中所有位置的高度都大于等于当前的高度,小蓝的滑行就结束了。
小蓝不能滑出矩阵所表示的场地。
小蓝可以任意选择一个位置开始滑行,请问小蓝最多能滑行多远距离。
输入格式
输入第一行包含两个整数 n, m,用一个空格分隔。
接下来 n 行,每行包含 m 个整数,相邻整数之间用一个空格分隔,依次表示每个位置的高度。
输出格式
输出一行包含一个整数,表示答案。
样例输入
4 5
1 4 6 3 1
11 8 7 3 1
9 4 5 2 1
1 3 2 2 1
样例输出
7
样例说明
滑行的位置一次为 (2, 1), (2, 2), (2, 3), (3, 3), (3, 2), (4, 2), (4, 3)。
评测用例规模与约定
对于 30% 评测用例,1 <= n <= 20,1 <= m <= 20,0 <= 高度 <= 100。
对于所有评测用例,1 <= n <= 100,1 <= m <= 100,0 <= 高度 <= 10000。
思路:dfs搜索,可以和第5题进行比较
import java.util.Scanner;
public class Main{
static int n, m;
static int[][] g = new int[110][110];
static int[][] f = new int[110][110];
static int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
static int dfs(int x, int y) {
if (f[x][y] != -1)
return f[x][y];
int res = 0;
for (int i = 0; i < 4; ++ i )
{
int tx = x + dx[i], ty = y + dy[i];
if (tx <= 0 || ty <= 0 || tx > n || ty > m || g[tx][ty] >= g[x][y])
continue;
res = Math.max(res, dfs(tx, ty));
}
f[x][y] = res + 1;
return f[x][y];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
for (int i = 1; i <= n; ++ i )
for (int j = 1; j <= m; ++ j )
g[i][j] = sc.nextInt();
for (int i = 1; i <= n; ++ i )
for (int j = 1; j <= m; ++ j )
f[i][j] = -1;
int res = 0;
for (int i = 1; i <= n; ++ i )
for (int j = 1; j <= m; ++ j )
res = Math.max(res, dfs(i, j));
System.out.println(res);
}
}
区间最小值
问题描述:小蓝有一个序列 a[1], a[2], …, a[n]。
给定一个正整数 k,请问对于每一个 1 到 n 之间的序号 i,a[i-k], a[i-k+1], …, a[i+k] 这 2k+1 个数中的最小值是多少?当某个下标超过 1 到 n 的范围时,数不存在,求最小值时只取存在的那些值。
输入格式
输入的第一行包含一整数 n。
第二行包含 n 个整数,分别表示 a[1], a[2], …, a[n]。
第三行包含一个整数 k 。
输出格式
输出一行,包含 n 个整数,分别表示对于每个序号求得的最小值。
样例输入
5
5 2 7 4 3
1
样例输出
2 2 2 3 3
评测用例规模与约定
对于 30% 的评测用例,1 <= n <= 1000,1 <= a[i] <= 1000。
对于 50% 的评测用例,1 <= n <= 10000,1 <= a[i] <= 10000。
对于所有评测用例,1 <= n <= 1000000,1 <= a[i] <= 1000000。
思路:暴力或单调队列
下面的讲解是暴力
import java.io.*;
import java.util.*;
import javax.swing.text.AbstractDocument.BranchElement;
public class Main {
static int n, k;
static int[][] f = new int[1000010][25];
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter wt = new PrintWriter(new OutputStreamWriter(System.out));
String[] s1 = bf.readLine().split(" ");
n = Integer.parseInt(s1[0]);
s1 = bf.readLine().split(" ");
for (int i = 1; i <= n; ++ i )
f[i][0] = Integer.parseInt(s1[i - 1]);
s1 = bf.readLine().split(" ");
k = Integer.parseInt(s1[0]);
for (int j = 1; j <= log2(n); ++ j )
for (int i = 1; i + (1 << j) - 1 <= n; ++ i )
f[i][j] = Math.min(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
for (int i = 1; i <= n; ++ i )
{
int l = Math.max(i - k, 1), r = Math.min(i + k, n);
int p = log2(r - l + 1);
wt.println(Math.min(f[l][p], f[r - (1 << p) + 1][p]) + " ");
}
wt.flush();
}
}
总结
算法的学习是一个水滴石穿的过程,厚积而薄发。
不断学习,不断做题,拓展思路,才能AC!一起努力一起加油!