试卷总分:100分
选择题 15题 30分
判断题 10题 20分
编程题 2题 50分
执行下面程序后,输出为( )。
int f(int x = 2) { return x * 3; } int main() { cout << f() << " " << f(4); }
执行下面代码后,输出为( )。
int main() { int a = 5; int* p = &a; int** q = &p; **q += 7; cout << a << " " << *p; }
已知:
int a[3][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }; int (*p)[4] = a;
则表达式 *(*(p + 2) + 1) 的值为( )。
void fun(int a, int &b, int *c) { a += 1; b += 2; *c += 3; } int main() { int x = 1, y = 1, z = 1; fun(x, y, &z); cout << x << " " << y << " " << z; }
执行下面程序后输出为( )。
int x = 3; void f(int& x) { x += 2; } int main() { int x = 10; f(x); cout << x << " " << ::x; }
执行下面代码后输出为( )。
struct S { int a; int b; }; void g(S s){ s.a += 10; } void h(S& s){ s.b += 10; } int main(){ S s{1,2}; g(s); h(s); cout << s.a << " " << s.b; }
执行 climb(6) 的返回值为( )。
int climb(int n) { if(n <= 2) return n; int a = 1, b = 2, c = 0; for(int i = 3; i <= n; i++) { c = a + b; a = b; b = c; } return c; }
某排序算法对如下数据排序(按 score 升序),则下面关于该排序算法稳定性的描述中,说法正确的是( )。
初始: (90,'A'), (90,'B'), (80,'C'), (90,'D')
排序后: (80,'C'), (90,'A'), (90,'B'), (90,'D')
下面代码试图把数组按升序进行“插入排序”,横线处应填写( )。
void ins(int a[], int n) { for(int i = 1; i < n; i++) { int key = a[i]; int j = i-1; while(j >= 0 && __________) { a[j+1] = a[j]; j--; } a[j+1] = key; } }
下列代码段的时间复杂度为( )。
int cnt=0; for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if( (i+j) % 3 == 0) cnt++; } }
执行下面程序,输出结果是( )。
int divi(int a,int b) { if(b==0) throw 0; return a/b; } int main() { try { cout << divi(10,0); } catch(const char* msg) { cout << "A"; } catch(int) { cout << "B"; } }
下列函数实现排行榜中单个元素的位置调整(类似插入排序的相邻搬移)。当某玩家分数增加,需将其向前移动时, while 循环的条件应为( )。
struct Player { int score; }; void up(Player players[], int n, int idx) { Player cur = players[idx]; int i = idx; while( ____________________ ) { players[i] = players[i-1]; i--; } players[i] = cur; }
下面代码执行结束时,变量 a 的值变成 15。
void add10(int &x) { x += 10; } int main() { int a = 5; add10(a); }
执行下面代码,输出结果为 5 。
int main() { int a[2][3]; cout << &a[1][2] - &a[0][1] << endl; return 0; }
下面程序可以正常编译并输出 10 。
int calc(int x, int y = 10); int calc(int x) { return x * 2; } int calc(int x, int y) { return x * y; } int main() { cout << calc(5); }
下面程序执行后输出 2010 。
int x = 10; void f() { int x = 20; cout << x; } int main() { f(); cout << x; }
下面代码没有语法错误。
struct GameCharacter { string name; int level; float position_x; float position_y; struct Equipment { string weapon; int attack_bonus; int defense_bonus; } equipment; struct Skill { string name; int damage; } skills[8]; int skill_count; };
下面程序能够把 Hello 写入 data.txt 文件中。
ofstream fout("data.txt"); cout << "Hello"; fout.close();
下面用递推方式计算斐波那契数列第 n 项的程序,时间复杂度是 O(2n)。
int fib(int n) { if (n <= 1) return n; int f0 = 0, f1 = 1, cur = 0; for (int i = 2; i <= n; i++) { cur = f0 + f1; f0 = f1; f1 = cur; } return cur; }
现有一片山地,可以视为一个 N 行 M 列的网格图,第 i 行 j 列的海拔为 hi,j。
如果一个单元格的海拔不高于其所有相邻单元格(相邻包括上、下、左、右、左上、右上、左下、右下,最多 8 个方向)的海拔,则称该单元格为山谷。
请你数一数该片山地中有多少山谷。
第一行包含2个整数N,M,表示山地的大小。
之后N行,每行包含M个整数h(i,1),h(i,2),...,h(i,M),表示海拔。
输出1行,包含1个整数C,表示山谷的数量。
保证1≤N,M≤100,1≤h(i,j)≤10^5
3 5
7 6 6 7 9
6 5 6 7 6
6 5 7 8 9
3
商店推出了许多礼盒,每个礼盒中包含 k 件商品,每件商品都有一个价格。
现在需要对这些礼盒进行排序,排序规则如下:
请输出排序后的礼盒编号。
第一行包含两个整数n和k,分别表示礼盒数量和每个礼盒中商品的数量。
接下来n行,每行包含k个整数,第i行表示第i个礼盒中各商品的价格。
输出一行,包含排序后的礼盒编号(编号从1开始),用空格分隔。
保证1≤n≤10^3,1≤k≤10,商品价格≤10^4。
4 3
3 5 2
4 1 5
2 2 4
3 4 3
3 4 2 1