题型:单选题
下面代码实现了冒泡排序函数,则横线上应填写( )。
//交换数组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; // 此轮“冒泡”未交换任何元素
}
} | A. for (int j = 0; j < arr.size() - 1; j++) |
B. for (int j = arr.size() - 1; j > 0; j–) |
| C. for (int j = 0; j < i; j++) |
D. for (int j = i-1; j <=0; j–) |
更新时间:2025-05-18 15:24:02
|
【知识点】
CCF—GESP C++四级
抱歉! 您未登录, 不能查看答案和解析点击登录
相似题推荐
判断题
下面程序执行后输出 2010 。
int x = 10;
void f() { int x = 20; cout << x; }
int main() {
f();
cout << x;
} | A.正确 | B.错误 |
2026-09-29
判断题
下面程序可以正常编译并输出 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);
} | A.正确 | B.错误 |
2026-09-29
判断题
下面程序能够把 Hello 写入 data.txt 文件中。
ofstream fout("data.txt");
cout << "Hello";
fout.close(); | A.正确 | B.错误 |
2026-09-29
单选题
执行 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;
} | A. 8 |
B. 13 |
| C. 5 |
D. 10 |
2026-09-29
单选题
已知:
int a[3][4] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
int (*p)[4] = a;则表达式 *(*(p + 2) + 1) 的值为( )。
| A. 6 |
B. 10 |
| C. 9 |
D. 11 |
2026-09-29
