int max(int i,int j){
if(i>j) return i;
return j;
}
void reverse(char* s,int head,int tail){
while(head<=tail){
char c=s[head];
s[head]=s[tail];
s[tail]=c;
head++;
tail--;
}
}
char* addBinary(char* a, char* b) {
int na=strlen(a),nb=strlen(b);
int n=max(na,nb);
reverse(a,0,na-1);
reverse(b,0,nb-1);
char* array=(char*)malloc(sizeof(char)*(n+4));
int pre=0,index=0;
while(index<n){
int x=0;
int y=0;
if(index<na) x=a[index]-'0';
if(index<nb) y=b[index]-'0';
char t=(x+y+pre)%2+'0';
array[index++]=t;
pre=(x+y+pre)/2;
}
if(pre==1) array[index++]='1';
array[index]=0;
reverse(array,0,index-1);
return array;
}
标签:head,return,二进制,求和,int,67 From: https://www.cnblogs.com/llllmz/p/18088173