正在载入在线练习界面,本页内容可直接阅读…
AK CSP › CSP-S 2023 第一轮真题 › 第17题
CSP-S 2023 第一轮 第17题:(2 分)将f函数的输入参数的类型改为 unsigned int,程序的输出不变
题目
#include <iostream>
using namespace std;
unsigned short f(unsigned short x) {
x ^= x << 6;
x ^= x >> 8;
return x;
}
int main() {
unsigned short x;
cin >> x;
unsigned short y = f(x);
cout << y << endl;
return 0;
}
假设输入的 $x$ 是不超过 $65535$ 的自然数,完成下面的判断题和单选题:本小题
(2 分)将 f 函数的输入参数的类型改为 unsigned int,程序的输出不变。()
选项
- T. 正确
- F. 错误
答案
F
题解
答案:F,错误。
按题目通常的约定,unsigned short 为 16 位。关键是:每次赋值给 unsigned short 时,都会截去超出 16 位的部分,而改成 unsigned int 后,截断的时机会改变。
例如输入 x = 1024:
| 执行步骤 | 原参数为 unsigned short | 改成 unsigned int |
|---|---|---|
x ^= x << 6 | 计算得 66560,赋值时截断为 1024 | 保留 66560 |
x ^= x >> 8 | 1024 ^ 4 = 1028 | 66560 ^ 260 = 66820 |
返回(返回类型仍为 unsigned short) | 1028 | 截断为 1284 |
第一步保留下来的高位会在第二步右移时影响低位,因此即使最后仍转换成 unsigned short,输出也可能不同。
真题版权归 CCF 所有,本站仅用于非商业教学用途。页面加载后可直接在线作答,作答记录保存在本浏览器或账号中。 京ICP备2026056990号-1
京公网安备11010502062986号