#CM10025. CSP-J 完善程序模拟(五)
CSP-J 完善程序模拟(五)
CSP-J 初赛完善程序专项模拟(五)
三、完善程序题(共2题,每题5空,每空3分,共计30分)
根据题目说明和已有代码,在空缺处填入正确的语句。每题均为四选一单选题。
题目一:二分——寻找等差数列中的缺失元素
【问题描述】
给定一个原本是等差数列的数组,数组长度为 n(n ≥ 3)。数组中的元素原本按照等差数列排列,但其中恰好缺失了一个元素(不在数组中出现),且数组顺序被打乱。请找出缺失的那个元素。
例如:
- 原等差数列:[1, 3, 5, 7, 9],缺失 5 后数组变为 [1, 3, 7, 9],输出 5
- 原等差数列:[2, 4, 6, 8, 10],缺失 6 后数组变为 [2, 4, 8, 10],输出 6
【算法原理】
- 首先将数组排序,恢复等差数列的顺序
- 计算完整的等差数列的公差:
d = min(a[1] - a[0], a[2] - a[1])- 注意:缺失元素可能出现在开头或结尾,因此需要从三个相邻差值中判断
- 使用二分查找找到缺失元素的位置:
- 如果
a[mid] == a[0] + mid * d,说明 mid 左侧没有缺失,缺失在右半部分 - 否则,缺失在左半部分(包括 mid)
- 如果
- 最终找到缺失元素的值
【程序】
#include <iostream>
#include <algorithm>
using namespace std;
int a[100005];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int d = (a[n - 1] - a[0]) / (n + 1);
int L = 0, R = n - 1;
while (L < R) {
int mid = (L + R) / 2;
if (a[mid] == a[0] + mid * d) {
L = mid + 1;
} else {
R = mid;
}
}
cout << a[0] + L * d << endl;
return 0;
}
1. 空缺 (1) 处应填入( )。{{ select(1) }}
a + 1, a + na, a + na + 1, a + n + 1a, a + n + 1
2. 空缺 (2) 处应填入( )。{{ select(2) }}
min(a[1] - a[0], a[2] - a[1])max(a[1] - a[0], a[2] - a[1])(a[n-1] - a[0]) / n(a[n-1] - a[0]) / (n + 1)
3. 空缺 (3) 处应填入( )。{{ select(3) }}
a[mid] - a[0] == d * mida[mid] == a[0] + mid * da[mid] - a[mid - 1] == da[mid] - a[0] == d * (mid - 1)
4. 空缺 (4) 处应填入( )。{{ select(4) }}
L = midL = mid + 1R = mid - 1R = mid
5. 空缺 (5) 处应填入( )。{{ select(5) }}
a[0] + L * da[L] + da[0] + (L + 1) * da[L] - d
题目二:BFS 洪水填充(图像渲染)
【问题描述】
给定一个 n × m 的二维图像矩阵 image,每个格子有一个颜色值(整数)。给定起始坐标 (sr, sc) 和新颜色 newColor,请将从 (sr, sc) 开始的所有与起始颜色相同的连通区域(四连通:上、下、左、右)的颜色改为 newColor,并返回修改后的图像矩阵。
例如:
- 输入:
image = [[1,1,1],[1,1,0],[1,0,1]],sr = 1, sc = 1, newColor = 2 - 输出:
[[2,2,2],[2,2,0],[2,0,1]]
【算法原理】
- 如果起始颜色等于新颜色,直接返回原图像
- 使用 BFS(广度优先搜索)遍历所有与起始位置颜色相同的连通格子
- 用队列存储待处理的格子坐标
- 每次从队列取出一个格子,将其颜色改为新颜色
- 检查四个方向的相邻格子,如果颜色与起始颜色相同且未访问过,则入队
【程序】
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int oldColor = image[sr][sc];
if (oldColor == newColor) return image;
int n = image.size(), m = image[0].size();
vector<vector<bool>> visited(n, vector<bool>(m, false));
queue<pair<int, int>> q;
q.push(make_pair(sr, sc));
visited[sr][sc] = true;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
int x = cur.first, y = cur.second;
image[x][y] = newColor;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m &&
image[nx][ny] == oldColor && !visited[nx][ny]) {
visited[nx][ny] = true;
q.push(make_pair(nx, ny));
}
}
}
return image;
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> image(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> image[i][j];
}
}
int sr, sc, newColor;
cin >> sr >> sc >> newColor;
vector<vector<int>> result = floodFill(image, sr, sc, newColor);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}
6. 空缺 (1) 处应填入( )。{{ select(6) }}
image[sr][sc] == newColorimage[sr][sc] != newColorsr == scnewColor == 0
7. 空缺 (2) 处应填入( )。{{ select(7) }}
{sr, sc}sr, sc{sr, sc, 0}make_pair(sr, sc)
8. 空缺 (3) 处应填入( )。{{ select(8) }}
q.pop()q.front()q.back()q.top()
9. 空缺 (4) 处应填入( )。{{ select(9) }}
image[nx][ny] == oldColor && !visited[nx][ny]image[nx][ny] != oldColor && !visited[nx][ny]image[nx][ny] == oldColor && visited[nx][ny]image[nx][ny] == newColor
10. 空缺 (5) 处应填入( )。{{ select(10) }}
returnreturn imagereturn 0return newColor