-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddBinary.cpp
48 lines (41 loc) · 978 Bytes
/
AddBinary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<string.h>
using namespace std;
class Solution {
public:
string addBinary(string a, string b) {
if(a.length()<b.length()) return addBinary(b, a);
int i=a.length()-1, j=b.length()-1;
int carry=0;
while(i>=0){
//int num;
if(j>=0){
//num=a[i]-'0'+b[j]-'0'+carry;
carry+=a[i]-'0'+b[j]-'0';
}else{
//num=a[i]-'0'+carry;
carry+=a[i]-'0';
}
//a[i]=num%2+'0';
//carry=num/2;
a[i]=carry%2+'0';
carry/=2;
i--;
j--;
}
if(carry){
//a.insert(0,1,'1');
a = '1'+a;
}
return a;
}
};
int main(){
string a, b;
cin>>a>>b;
string result;
Solution *solution=new Solution();
result=solution->addBinary(a,b);
cout<<result;
return 0;
}