Codeforces Round #696 (Div. 2) 寒假訓練題解
技術標籤:練習總結
A. Puzzle From the Future
In the 2022 year, Mike found two binary integers a and b of length n (both of them are written only by digits 0 and 1) that can have leading zeroes. In order not to forget them, he wanted to construct integer d in the following way:
he creates an integer c as a result of bitwise summing of a and b without transferring carry, so c may have one or more 2-s. For example, the result of bitwise summing of 0110 and 1101 is 1211 or the sum of 011000 and 011000 is 022000;
Unfortunately, Mike lost integer a before he could calculate d himself. Now, to cheer him up, you want to find any binary integer a of length n such that d will be maximum possible as integer.
Maximum possible as integer means that 102>21, 012<101, 021=21 and so on.
Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.
The first line of each test case contains the integer n (1≤n≤105) — the length of a and b.
The second line of each test case contains binary integer b of length n. The integer b consists only of digits 0 and 1.
It is guaranteed that the total sum of n over all t test cases doesn’t exceed 105.
Output
For each test case output one binary integer a of length n. Note, that a or b may have leading zeroes but must have the same length n.
解題思路: 直接做就完事了,剛開始錯把陣列型別設成了int,導致一整個數字字串被當成整型陣列輸入了。
#include<iostream>
using namespace std;
char b[100005],a[100005];
int main(){
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
for(long long j=0;j<n;j++) cin>>b[j];
a[0]='1';
for(long long i=1;i<n;i++){
if(a[i-1]-'0'+b[i-1]-'0'==1&&b[i]=='1') a[i]='1';
else if(a[i-1]-'0'+b[i-1]-'0'==1&&b[i]=='0') a[i]='0';
else if(a[i-1]-'0'+b[i-1]-'0'==2&&b[i]=='1') a[i]='0';
else if(a[i-1]-'0'+b[i-1]-'0'==2&&b[i]=='0') a[i]='1';
else if(a[i-1]-'0'+b[i-1]-'0'==0&&b[i]=='0') a[i]='1';
else if(a[i-1]-'0'+b[i-1]-'0'==0&&b[i]=='1') a[i]='1';
}
for(long long i=0;i<n;i++) cout<<a[i];
cout<<endl;
}
return 0;
}