在 C++ 中,( )正确定义了一个返回整数值并接受两个整数参数的函数。
int add(int a, int b) { return a + b; }
void add(int a, int b) { return a + b; }
int add(a, b) { return a + b; }
void add(int a, int b) { return a - b; }
在C++中,形参与实参的关系描述正确的是( )。
形参在函数调用时指定,实参在函数定义时传递
形参在函数定义时指定,实参在函数调用时传递
形参和实参可以互换
形参和实参必须是完全相同的类型,不能有任何差异。
运行以下代码,屏幕上将输出( )。
#include <iostream>
using namespace std;
int var = 100;
void function() {
int var = 200;
cout << var << " ";
cout << ::var << " ";
}
int main() {
cout << var << " ";
function();
var += 100;
cout << var << " ";
return 0;
}
100 200 100 200
100 200 100 300
100 200 200 200
100 200 200 300
运行下面代码,屏幕上输出是( )。
int arr[3] = {24, 9, 7};
int* p = arr;
p++;
cout << *p << endl;
24
9
7
不确定
运行下面代码片段的结果是( )。
int x = 20;
int y = 24;
int* p = &x;
int* q = &y;
p = q;
将x赋值为24
将y赋值为20
将q指向x的地址
将p指向y的地址
在 C++ 中,( )正确定义一个名为 student 的结构体,其中包含一个 name 字符数组和一个 age 整数?
struct student { char name[20]; int age; };
student struct { char name[20]; int age; };
student struct { string name; int age; };
struct student { char[20] name; int age; };
在 C++ 中,( )正确声明了一个 3 行 4 列的二维数组。
int arr[3, 4];
int arr[3][4];
int arr[4][3];
int arr(3, 4);
一个二维数组定义为 int arr[3][4]; (假设一个int变量占4个字节),则 int arr[0] 占用( )个字节的内存。
3
4
12
16
下面代码采用递推算法来实现整数 的阶乘( ),则横线上应填写( )。
int factorial(int n) {
int result = 1;
for (int i = 2; i <= n; i++) {
________________________________ // 在此处填入代码
}
return result;
}
result *= i;
result += i;
result *= result;
result += result;
在排序算法中,稳定性指的是( )。
排序后数据不会丢失
排序后相同元素的相对顺序保持不变
排序后数据不会被修改
排序后数据的时间复杂度不变
下面代码实现了冒泡排序函数,则横线上应填写( )。
//交换数组arr的第i个元素和第j个元素
void swap(vector<int> &arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
int bubble_sort(vector<int> &arr) {
for (int i = arr.size() - 1; i > 0; i--) {
bool flag = false; // 标志位
________________________________ { // 在此处填入代码
if (arr[j] > arr[j + 1]) {
swap(arr, i, j);
flag = true;
}
}
if (!flag)
break; // 此轮“冒泡”未交换任何元素
}
}
for (int j = 0; j < arr.size() - 1; j++)
for (int j = arr.size() - 1; j > 0; j--)
for (int j = 0; j < i; j++)
for (int j = i-1; j <=0; j--)
上一题算法的时间复杂度为( )。
O(n^2)
O(2^n)
O(1)
O(n)
下面代码实现了插入排序函数(升序),则横线上应填写( )。
void insertion_sort(vector<int> &nums) {
for (int i = 1; i < nums.size(); i++) {
int base = nums[i], j = i - 1;
________________________________ { // 在此处填入代码
nums[j + 1] = nums[j];
j--;
}
nums[j + 1] = base;
}
}
while (j >= 0 && nums[j] > base)
while (j > 0 && nums[j] > base)
while (j >= 0 && nums[j] < base)
while (j > 0 && nums[j] < base)
小杨用文件重定向实现在 log.txt 文件中输出日志,则下面横线上应填写( )。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ofstream log_file("log.txt");
streambuf* original_cout = cout.rdbuf();
cout.rdbuf(log_file.rdbuf());
___________________________________ // 在此处填入代码
cout.rdbuf(original_cout); // 恢复原始的标准输出缓冲区
return 0;
}
cout << "This output will go to the log file." << endl;
log_file << "This output will go to the log file." << endl;
cout >> "This output will go to the log file." >> endl;
log_file >> "This output will go to the log file." >> endl;
运行下面的代码,屏幕上将输出( )。
#include <iostream>
using namespace std;
int divide(int a, int b) {
if (b == 0) {
throw runtime_error("division by zero error ");
}
return a / b;
}
int main() {
int x = 10;
int y = 0; // 设为 0 会导致除零错误
try {
int result = divide(x, y);
cout << "result: " << result << endl;
} catch (const runtime_error& e) {
cout << "caught an exception: " << e.what() << endl;
}
return 0;
}
division by zero error result: caught an exception:
result: caught an exception: division by zero error
caught an exception: division by zero error
division by zero error caught an exception: division by zero error
代码 int a = 10; int* p = &a; 可以正确定义指针和初始化指针。
在 C++ 中,引用传递允许函数修改传递给它的参数的值。
指针的大小与其所指向的变量的数据类型的大小相同。
二维数组的行的大小的必须在定义时确定,列的大小可以动态变化。
递推算法通过逐步求解当前状态和前一个或几个状态之间的关系来解决问题。
选择排序是稳定的排序算法。
插入排序的时间复杂度总是比冒泡排序低。
在 C++ 中,如果没有捕获到异常(没有匹配的 catch 块),程序会继续执行而不会终止。
以下代码用递推法求斐波那契数列的第 项,时间复杂度为指数级。
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
int f0 = 0; // F(0)
int f1 = 1; // F(1)
int current;
for (int i = 2; i <= n; i++) {
current = f0+ f1; // F(n) = F(n-1) + F(n-2)
f0 = f1;
f1 = current;
}
return current;
}
执行下面C++代码后,输出的是20。
int point(int* p){
return *p * 2;
}
int main() {
int a = 10;
int* p = &a;
*p = point(p);
cout << *p << endl;
}
黑白方块
题目描述
小杨有一个n行m列的网格图,其中每个格子要么是白色,要么是黑色。
小杨想知道网格图中是否存在一个满足如下条件的子矩形:
子矩形由4行4列组成;
子矩形的第1行和第4行只包含白色格子;
对于子矩形的第2行和第3行,只有第1个和第4个格子是白色的,其余格子都是黑色的;
请你编写程序帮助小杨判断。
输入格式
第一行包含一个正整数t,代表测试用例组数。
接下来是 组测试用例。对于每组测试用例,一共n+1行。
第一行包含两个正整数n,m,含义如题目所示。
之后n行,每行一个长度为m的01串,代表网格图第i行格子的颜色,如果为0,则对应格子为白色,否则为黑色。
输出格式
对于每组测试用例,如果存在,输出 Yes,否则输出 No。
样例1
输入
3
1 4
0110
5 5
00000
01100
01100
00001
01100
5 5
00000
01100
01110
00001
01100
输出
No
Yes
No
满足条件的子矩形形如:
0000
0110
0110
0000
对于全部数据,保证有1≤t≤10,1≤n,m≤100。
区间排序
题目描述
小杨有一个包含n个正整数的序列a。
小杨计划对序列进行多次升序排序,每次升序排序小杨会选择一个区间[l,r](l≤r)并对区间内所有数字,即al,al+1,,,ar进行升序排序。每次升序排序会在上一次升序排序的结果上进行。
小杨想请你计算出多次升序排序后的序列。
输入格式
第一行包含一个正整数n,含义如题目所示。
第二行包含n个正整数 ,al,al+1,,,an代表序列。
第三行包含一个正整数 ,代表排序次数。
之后q行,每行包含两个正整数li,ri,代表将区间[li,ri]内所有数字进行升序排序。
输出格式
输出一行包含n个正整数,代表多次升序排序后的序列。
样例1
输入
5
3 4 5 2 1
3
4 5
3 4
1 3
输出
1 3 4 5 2 1
第一次升序排序后,序列为[3,4,5,1,2];
第二次升序排序后,序列为[3,4,1,5,2];
第三次升序排序后,序列为[1,3,4,5,2];
对于全部数据,保证有1≤n≤100,1≤ai≤100,1≤q≤100,1≤li≤ri≤n。