牛客 QQ4 游戏任务标记
概述
https://www.nowcoder.com/practice/2f45f0ef94724e06a4173c91ef60781c
考察位运算。
位运算
- 设置右起第 n 位为 1:
a = a | 1 << n
- 检查右起第 n 为是否为 1:
(a >> n) % 2 == 1
#include <iostream>
using namespace std;
int main() {
uint t[32] = {};
int a, b;
cin >> a >> b;
if (a <=0 || a > 1024 || b <= 0 || b > 1024) {
cout << -1;
} else {
int x = (a - 1) / 32;
int y = (a - 1) % 32;
t[x] = t[x] | 1 << y;
int m = (b - 1) / 32;
int n = (b - 1) % 32;
int res = (t[m] >> n) % 2 == 1;
cout << res;
}
}
Links: 牛客-qq04-游戏任务标记