题解 AT4239 [ABC105C] Base -2 Number
题意
给定一个整数
思路
和转换为 vector 保存答案。输出时将所有
这里
代码
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> v; // 保存答案
int main() {
cin >> n;
if (n == 0) {
puts("0");
return 0;
}
int cnt = -1;
while(n) { // 短除
v.push_back(n % (-2));
cnt++;
n /= (-2);
if (v[cnt] == -1) n++;
}
for(int i = v.size() - 1; i >= 0; --i) printf("%d", v[i] == -1 ? 1 : v[i]);
return 0;
}