模拟求优化,若思路有误,期望改正。
#include <iostream>
#include <algorithm>
using namespace std;
string change(int x) {
string res = "";
while (x) {
string p = to_string(x%2);
res += p;
x >>= 1;
}
reverse(res.begin(), res.end());
return res;
}
string pl(string a, string b) {
reverse(a.begin(), a.end()), reverse(b.begin(), b.end());
int i = 0, j = 0, jw = 0;
string ans = "";
while (i<a.size() || j<b.size() || jw) {
int ii = i<a.size() ? a[i]-'0' : 0, jj = j<b.size() ? b[j]-'0' : 0;
int s = ii+jj+jw;
jw = s>>1;
ans += s%2 + '0';
++i, ++j;
}
reverse(ans.begin(), ans.end());
return ans;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
string s = "";
while (n--) {
int op;
scanf("%d", &op);
if (op == 1) s += "0";
else {
int d;
scanf("%d", &d);
string str = change(d);
s = pl(s, str);
}
}
cout << s << endl;
}
return 0;
}