/
AddBinary.cpp
executable file
·52 lines (46 loc) · 1.17 KB
/
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
49
50
51
52
/*************************************************************************
> File Name: AddBinary.cpp
> Author:
> Mail:
> Created Time: 2015年06月29日 星期一 19时23分23秒
************************************************************************/
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class Solution
{
public:
void process(string& a, string& b)
{
int sizeA = a.size();
int sizeB = b.size();
if(sizeA > sizeB) b.insert(0, sizeA-sizeB, '0');
else if(sizeB > sizeA) a.insert(0, sizeB-sizeA, '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
}
string addBinary(string a, string b)
{
process(a, b);
int flag = 0;
int size = a.size();
for(int i=0; i<size; ++i)
{
int temp = (a[i]-'0')+(b[i]-'0')+flag;
a[i] = (temp%2)+'0';
flag = (temp>=2 ? 1 : 0);
}
if(flag == 1) a.append("1");
reverse(a.begin(), a.end());
return a;
}
};
int main()
{
Solution sol;
string a = "11";
string b = "1";
cout<<sol.addBinary(a, b)<<endl;
return 0;
}