struct Player {
string name;
int score;
};
// 玩家索引playerIdx的分数刚刚更新,需要调整位置
void updateRanking(Player players[], int size, int playerIdx) {
Player updatedPlayer = players[playerIdx];
if (playerIdx > 0 && updatedPlayer.score > players[playerIdx - 1].score) {
int i = playerIdx;
while (____________________) { // 在此处填入代码
players[i] = players[i - 1];
i--;
}
players[i] = updatedPlayer;
} else if (playerIdx < size - 1 && updatedPlayer.score < players[playerIdx + 1].score) {
int i = playerIdx;
while (____________________) { // 在此处填入代码
players[i] = players[i + 1];
i++;
}
players[i] = updatedPlayer;
}
}
2分
登录后查看选项
13
给定如下算法,其时间复杂度为( )。
bool f(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
sum += arr[j];
}
}
if (sum == target) return true;
}
return false;
}
int divide(int a, int b) {
if(b == 0) throw "Division by zero";
return a / b;
}
int main() {
int result = 0;
try {
result = divide(10, 0);
cout << "A";
} catch(const char* msg) {
cout << "B";
result = -1;
}
cout << result;
return 0;
}
2分
登录后查看选项
判断题 共10道
16
小杨正在调试他的温度传感器程序,其中变量 x 保存当前温度。下面这段代码运行后,变量 x 的值变成了 8 。
int x = 5;
int *p = &x;
*p = *p + 3;
2分
登录后查看选项
17
一个结构体不能包含另一个结构体。
2分
登录后查看选项
18
在 C++ 中,定义如下二维数组: int a[3][4]; ,数组 a 在内存中是按行优先连续存放的,即 a[0][0] 、 a[0][1] 、 a[0][2] 、 a[0][3] 在内存中是连续的。
2分
登录后查看选项
19
执行下面程序后,变量 a 的值会变成 15 。
void add(int &x){
x += 10;
}
int a = 5;
add(a);
2分
登录后查看选项
20
执行下面的C++代码,会输出 8 ,因为两个指针地址相差 8 个字节(假设 int 占 4 字节)。
int n = 10;
int f[20];
f[0] = 0;
f[1] = 1;
for (int i = 2; i <= n; i++)
f[i] = f[i - 1] + f[i - 2];
2分
登录后查看选项
22
冒泡排序和插入排序都是稳定排序算法。
2分
登录后查看选项
23
下面这段代码实现了选择排序算法。
void sort(int a[], int n) {
for (int i = 1; i < n; i++) {
int x = a[i];
int j = i - 1;
while (j >= 0 && a[j] > x) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = x;
}
}
2分
登录后查看选项
24
下面代码可以正常编译并输出 10 。
#include <iostream>
using namespace std;
int calculate(int x, int y = 10);
int main() {
cout << calculate(5); // 调用1
return 0;
}
int calculate(int x, int y) {
return x * y;
}
int calculate(int x) { // 重载函数
return x * 2;
}
2分
登录后查看选项
25
执行下面代码会输出 100 。
int main() {
ofstream fout("data.txt");
fout << 10 << " " << 20 << endl;
fout << 30 << " " << 40;
fout.close();
ifstream fin("data.txt");
int a, b, c, d;
fin >> a >> b >> c >> d;
fin.close();
cout << a + b + c + d;
return 0;
}
2分
登录后查看选项
编程题 共2道
26
建造
题目描述
小 A 有一张 M 行 N 列的地形图,其中第 i 行第 j 列的数字 aij 代表坐标 (i, j) 的海拔高度。
停机坪为一个 3×3 的区域且内部所有 9 个点的最大高度和最小高度之差不超过 H。
小 A 想请你计算出,在所有适合建造停机坪的区域中,区域内部 9 个点海拔之和最大是多少。
输入
第一行三个正整数 M,N,H,含义如题面所示。
之后 M 行,第 i 行包含 N 个整数a(i,1),a(i,2),...,a(i,N),代表坐标 (i,j) 的高度。
数据保证总存在一个适合建造停机坪的区域。
输出
输出一行,代表最大的海拔之和。
数据范围
对于所有测试点,保证 1≤M,N≤10^3,1≤H,a(i,j),≤10^5。
输入样例1
5 5 3
5 5 5 5 5
5 1 5 1 5
5 5 5 5 5
5 2 5 2 5
3 5 5 5 2
输出样例1
40
25分
登录后作答
27
优先购买
题目描述
小 A 有 M 元预算。商店有 N 个商品,每个商品有商品名 S、价格 P 和优先级 V 三种属性,其中 V 为正整数,且 V 越小代表商品的优先级越高。