#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#include <string>
int n;
typedef long long LL;
int main()
{
scanf("%d", &n);
for (int i = 0; i<n; i++)
{
string s;
cin>>s;
int pos = 31;
int m = 1; //进位符
while(pos >= 0)
{
if (s[pos] == '1' && m == 1)
{
s[pos] = '0';
m = 1;
}
else if (s[pos] == '0' && m == 1)
{
s[pos] = '1';
m = 0;
}
pos--;
}
if (m) s.insert(0,"1");
cout<<s<<endl;
if(s.size() == 32) pos = 30;
else pos = 31;
m = 1;
while(pos >= 0)
{
if (s[pos] == '1' && m == 1)
{
s[pos] = '0';
m = 1;
}
else if (s[pos] == '0' && m == 1)
{
s[pos] = '1';
m = 0;
}
pos--;
}
if (m) s.insert(0,"1");
cout<<s<<endl;
}
return 0;
}
感觉代码没有像别的朋友那么简洁,总之就是用字符串直接操作。先加1再加2,倒着遍历字符串两次,对每一位的0,1进行改变。m为进位符,来判断是否进位。
注意第二次加2的时候,要判断一下加1后的s是否变成了33位,来定位下一次遍历的pos在30位置还是31.