/**********************************************************************************
*
* Given two binary strings, return their sum (also a binary string).
*
* For example,
* a = "11"
* b = "1"
* Return "100".
*
**********************************************************************************/
#include <iostream>
#include <string>
using namespace std;
string addBinary(string a, string b) {
int alen = a.size();
int blen = b.size();
bool carry = false; //进位标志
string result;
while( alen>0 || blen>0) {
int abit = alen<=0 ? 0 : a[alen-1]-'0';
int bbit = blen<=0 ? 0 : b[blen-1]-'0';
int cbit = carry ? 1 : 0;
result.insert(result.begin(), '0' + ((abit+bbit+cbit) & 1) );
carry = (abit+bbit+cbit>1);
alen--; blen--;
}
if (carry){
result.insert(result.begin(), '1');
}
return result;
}
int main(int argc, char** argv)
{
string a = "11";
string b = "1";
if (argc>2){
a = argv[1];
b = argv[2];
}
cout << a << "+" << b << "=" << addBinary(a, b) << endl;
}
addBinary
最新推荐文章于 2024-09-08 20:02:08 发布