1. 题目来源
链接:3554. 二进制
2. 题目解析
显然不需要高精度。直接开 long long
存储即可。+3 操作最多会有 33 位的数。特判一下最高位的 33 位是否为 0,为 0 不用打印,为 1 打印输出即可。
时间复杂度:
O
(
n
)
O(n)
O(n)
空间复杂度:
O
(
1
)
O(1)
O(1)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
typedef long long LL;
void print(LL x) {
for (int i = 32; ~i; i -- ) {
int u = x >> i & 1;
if (i == 32 && !u) continue; // 如果第 33 位是 0 的话,不需要打印
cout << u;
}
cout << endl;
}
int main() {
string s;
int T;
scanf("%d", &T);
while (T -- ) {
cin >> s;
LL t = 0, p = 1;
for (auto e : s) t = t * 2 + e - '0';
print(t + 1);
print(t + 3);
}
return 0;
}