Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite75b429

Browse files
committed
Create 0067-add-binary.c
1 parentb3d1baa commite75b429

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎c/0067-add-binary.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
char*addBinary(constchar*a,constchar*b) {
2+
intmaxLen=strlen(a)>strlen(b) ?strlen(a) :strlen(b);
3+
char*res= (char*)malloc((maxLen+2)*sizeof(char));
4+
memset(res,0, (maxLen+2)*sizeof(char));
5+
unsignedintcarry=0;
6+
7+
for(inti=0;i<maxLen;i++) {
8+
unsignedintbitA=i<strlen(a) ?a[strlen(a)-i-1]-'0' :0;
9+
unsignedintbitB=i<strlen(b) ?b[strlen(b)-i-1]-'0' :0;
10+
11+
unsignedinttotal=bitA+bitB+carry;
12+
charsum='0'+total %2;
13+
carry=total /2;
14+
15+
// Add to the beginning of the string
16+
memmove(res+1,res,strlen(res));
17+
res[0]=sum;
18+
}
19+
if(carry) {
20+
memmove(res+1,res,strlen(res));
21+
res[0]='1';
22+
}
23+
returnres;
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp