特好的题目,进制转化就刷它。
void reverse(char* s){
int n=strlen(s);
int head=0,tail=n-1;
while(head<=tail){
char t=s[head];
s[head]=s[tail];
s[tail]=t;
head++;
tail--;
}
}
int max(int i,int j){
if(i>j) return i;
return j;
}
char* addBinary(char* a, char* b) {
reverse(a);
reverse(b);
int na=strlen(a),nb=strlen(b);
int n=max(na,nb);
char* array=(char*)malloc(sizeof(char)*(n+2));
int pre=0;
for(int i=0;i<n;i++){
if(i<na) pre+=a[i]-'0';
if(i<nb) pre+=b[i]-'0';
array[i]=pre%2+'0';
pre/=2;
}
if(pre==1){
array[n]='1';
array[n+1]=0;
}else{
array[n]=0;
}
reverse(array);
return array;
}
结果:
标签:reverse,二进制,na,char,求和,int,67,strlen From: https://www.cnblogs.com/llllmz/p/18076208