正在载入在线练习界面,本页内容可直接阅读…
AK CSP › CSP-S 2024 第一轮真题 › 第19题
CSP-S 2024 第一轮 第19题:函数intlogic(int×,int y)的功能是(
题目
#include <iostream>
using namespace std;
const int N = 1000;
int c[N];
int logic(int x, int y) {
return (x & y) ^ ((x ^ y) | (~x & y));
}
void generate(int a, int b, int *c) {
for (int i = 0; i < b; i++)
c[i] = logic(a, i) % (b + 1);
}
void recursion(int depth, int *arr, int size) {
if (depth <= 0 || size <= 1) return;
int pivot = arr[0];
int i = 0, j = size - 1;
while (i <= j) {
while (arr[i] < pivot) i++;
while (arr[j] > pivot) j--;
if (i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++; j--;
}
}
recursion(depth - 1, arr, j + 1);
recursion(depth - 1, arr + i, size - i);
}
int main() {
int a, b, d;
cin >> a >> b >> d;
generate(a, b, c);
recursion(d, c, b);
for (int i = 0; i < b; ++i) cout << c[i] << " ";
cout << endl;
}本小题
函数 int logic(int x, int y) 的功能是( )
选项
- A. 按位与
- B. 按位或
- C. 按位异或
- D. 以上都不是
答案
B
题解
答案是 B. 按位或。
位运算可以逐位分析,& 是与,| 是或,^ 是异或,~ 是取反。对 x、y 的某一位列真值表:
| x | y | x & y | (x ^ y) \ | (~x & y) | 最终结果 |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 0 | 1 | 1 | |
| 1 | 0 | 0 | 1 | 1 | |
| 1 | 1 | 1 | 0 | 1 |
可以看到,只有两位都为 0 时,结果才为 0;否则为 1,这正是按位或的规则。
所以函数等价于:
``cpp int logic(int x, int y) { return x | y; } ``
本题只问 logic 的功能,其他函数不影响答案。
真题版权归 CCF 所有,本站仅用于非商业教学用途。页面加载后可直接在线作答,作答记录保存在本浏览器或账号中。 京ICP备2026056990号-1
京公网安备11010502062986号