GESP-C++四级 题库
小杨今年春节回奶奶家了,奶奶家的数字电视要设置ip地址并接入到WIFI盒子才能收看节目,那这个WIFI盒子具有路由器的功能。( )
在C++中,两个字符串相加的运算符为+相当于字符串的合并运算。下面C++代码执行后,将输出chenadai 。( )

小杨最近在准备考GESP,他用的Dev C++来练习和运行程序,所以Dev C++也是一个小型操作系统。( )
域名是由一串用点分隔的名字来标识互联网上一个计算机或计算机组的名称,CCF 编程能力等级认证官方网站的域名是 gesp.ccf.org.cn,其中顶级域名是gesp。
建造
时间限制:1.0 s
内存限制:512.0 MB
题目描述
小 A 有一张M行N列的地形图,其中第i 行第 列的数字aij代表坐标 ( i,j)) 的海拔高度。停机坪为一个3*3的区域且内部所有 9个点的最大高度和最小高度之差不超过H。 小 A 想请你计算出,在所有适合建造停机坪的区域中,区域内部9个点海拔之和最大是多少。
输入格式
第一行三个正整数M,N,H,含义如题面所示。之后M行,第i行包含N个整数ai1,ai2,...,aiN,代表坐标(i,j)的高度。数据保证总存在一个适合建造停机坪的区域。
输出格式
输出一行,代表最大的海拔之和。
输入样例
输出样例
数据范围

#include <iostream>
using namespace std;
int a[1010][1010];
int main() {
int M, N, H;
cin >> M >> N >> H;
for (int i = 1; i <= M; ++i) {
for (int j = 1; j <= N; ++j) {
cin >> a[i][j];
}
}
int max_sum = 0;
for (int i = 1; i <= M - 2; ++i) {
for (int j = 1; j <= N - 2; ++j) {
int local_max = a[i][j], local_min = a[i][j], local_sum = 0;
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < 3; ++y) {
int height = a[i + x][j + y];
local_sum += height;
if (height > local_max)
local_max = height;
if (height < local_min)
local_min = height;
}
}
if (local_max - local_min <= H) {
if (local_sum > max_sum) {
max_sum = local_sum;
}
}
}
}
cout << max_sum;
return 0;
}优先购买
时间限制:1.0 s
内存限制:512.0 MB
题目描述
小 A 有M元预算。商店有N个商品,每个商品有商品名S 、价格P和优先级V三种属性,其中V为正整数,且V越小代表商品的优先级越高。
小 A 的购物策略为:
总是优先买优先级最高的东西;
如果有多个最高优先级商品,购买价格最低的;
如果有多个优先级最高且价格最低的商品,购买商品名字典序最小的。
小 A 想知道能购买哪些商品。
输入格式
第一行两个正整数 M,N,代表预算和商品数。
之后N行,每行一个商品,依次为Si,Pi,Vi ,代表第 个商品的商品名、价格、优先级。数据保证不存在两个名字相同的商品。
输出格式
按照字典序从小到大的顺序,输出所有购买商品的商品名。
输入样例
输出样例
数据范围
对于所有测试点,保证
商品名仅由小写字母组成且不存在两个相同的商品名。
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <assert.h>
using namespace std;
struct Item {
string name;
int price;
int priority;
};
bool cmp(const Item &a, const Item &b) {
if (a.priority != b.priority)
return a.priority < b.priority;
if (a.price != b.price)
return a.price < b.price;
return a.name < b.name;
}
string s[1005];
std::map<string, int> mp;
int main() {
int M, N;
cin >> M >> N;
Item items[N];
for (int i = 0; i < N; ++i) {
cin >> items[i].name >> items[i].price >> items[i].priority;
assert(mp.find(items[i].name) == mp.end());
mp[items[i].name] = items[i].priority;
}
sort(items, items + N, cmp);
int budget = M;
int count = 0;
for (int i = 0; i < N; ++i) {
if (items[i].price <= budget) {
budget -= items[i].price;
s[count++] = items[i].name;
}
}
sort(s, s + count);
for (int i = 0; i < count; ++i) {
cout << s[i] << endl;
}
return 0;
}排兵布阵


#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 15;
int n, m;
int a[N][N], col[N][N];
int ans;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
col[j][i] = col[j][i-1] + a[i][j]; // 第j列前缀和
}
for (int u = 1; u <= n; u++)
for (int l = 1; l <= m; l++)
for (int d = u; d <= n; d++) {
int chk = 1;
for (int r = l; r <= m; r++) {
// u~d行总和等于行数则全1
if (col[r][d] - col[r][u-1] != d - u + 1) {
chk = 0;
}
if (!chk) break;
ans = max(ans, (r - l + 1) * (d - u + 1));
}
}
printf("%d\n", ans);
return 0;
}最长连续段


#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 1e5 + 5;
int n;
int a[N];
int last, cnt, mx;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
last = a[1];
cnt = mx = 1;
// 从第二个元素开始遍历
for (int i = 2; i <= n; i++) {
if (a[i] == last) {
// 重复数字,连续长度不变
} else if (a[i] == last + 1) {
cnt++;
} else {
cnt = 1;
}
last = a[i];
mx = max(cnt, mx);
}
printf("%d\n", mx);
return 0;
}画布裁剪
时间限制:1 s,内存限制:512 MB
【问题描述】
小 A 在高为 h 宽为 w 的矩形画布上绘制了一幅画。由于画布边缘留白太多,小 A 想适当地裁剪画布,只保留画的主体。
具体来说,画布可以视为 h 行 w 列的字符矩阵,其中的字符均为 ASCII 码位于 33~126 之间的可见字符,小 A只保留画布中由第 x1 行到第 x2 行、第 y1 列到第 y2 列构成的子矩阵。
小 A 将画布交给了你,你能帮他完成画布的裁剪吗?
【输入描述】
第一行,两个正整数 h、w,分别表示画布的行数与列数。
第二行,四个正整数 x1、x2、y1、y2,表示保留的行列边界。
接下来 h 行,每行一个长度为 w 的字符串,表示画布内容。
【输出描述】
输出共 x2 - x1 + 1 行,每行一个长度为 y2 - y1 + 1 的字符串,表示裁剪后的画布。
【样例输入1】
【样例输出1】
【样例输入2】
【样例输出2】
【数据范围】
对于所有测试点,保证 1 <= h、w <= 100,1 <= x1 <= x2 <= h,1 <= y1 <= y2 <= w。
#include <iostream>
#include <cstring>
using namespace std;
int n, m;
string s[105];
int x1, x2, y1, y2;
int main() {
cin >> n >> m >> x1 >> x2 >> y1 >> y2;
for (int i = 0; i < n; i++)
cin >> s[i];
for (int i = x1 - 1; i < x2; i++) {
string s1 = s[i].substr(y1 - 1, y2 - y1 + 1);
cout << s[i].substr(y1 - 1, y2 - y1 + 1) << endl;
}
return 0;
}