标签:
比较简单,细节:先将字符串翻转,注意进位。
string addBinary(string a, string b) { string result; int len = a.size() > b.size() ? a.size() : b.size(); reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); int carry = 0; for (int i = 0; i < len; i++) { int ai = i < a.size() ? a[i] - ‘0‘ : 0; int bi = i < b.size() ? b[i] - ‘0‘ : 0; int val = (ai + bi + carry) % 2; carry = (ai + bi + carry) / 2; result.insert(result.begin(), val + ‘0‘); } if (carry == 1)result.insert(result.begin(), ‘1‘); return result; }
标签:
原文地址:http://www.cnblogs.com/573177885qq/p/5519595.html