string addBinary(string a, string b) { int carry=0; int n=a.size()>b.size()?a.size():b.size(); //最长的那个 reverse(a.begin(),a.end()); reverse(b.begin(),b.end()); string sum; for(size_t i=0;i<n;++i) { int ai,bi,val; ai=(i<a.size()?a[i]-'0': 0); bi=(i<b.size()?b[i]-'0': 0); val=(ai+bi+carry)%2; carry=(ai+bi+carry)/2; sum.insert(sum.begin(),val+'0'); } if(carry==1) sum.insert(sum.begin(),'1'); return sum; }