-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd Binary.cpp
55 lines (48 loc) · 1.06 KB
/
Add Binary.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
49
50
51
52
53
54
55
#include<iostream>
#include<cstring>
using namespace std;
string addBinary(string a, string b) {
int lena=a.size();
int lenb=b.size();
int re[max(lena,lenb)+1];
re[0]=0;
int i=0;
for (i=0;i<min(lena,lenb);i++)
{
re[max(lena,lenb)-i]=a[lena-1-i]-'0'+b[lenb-1-i]-'0';
}
i--;
while (i<lenb-1){
i++;
re[max(lena,lenb)-i]=b[lenb-1-i]-'0';
}
while (i<lena-1){
i++;
re[max(lena,lenb)-i]=a[lena-1-i]-'0';
}
for (int j=max(lena,lenb);j>0;j--)
{
if (re[j]>1)
{
re[j-1]+=re[j]/2;
re[j]=re[j]%2;
}
}
string res;
for (int k=max(lena,lenb);k>=0;k--)
{
res.push_back('0'+re[max(lena,lenb)-k]);
}
if (re[0]==0)
{
res=res.substr(1);
}
return res;
}
int main()
{
string a="0",b="1";
string c=addBinary(a,b);
cout<<c;
return 0;
}