Skip to content

Commit

Permalink
이슈 #368에서 솔루션 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Nov 21, 2024
1 parent 0ec7314 commit 4802f56
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Programmers/이진수_더하기.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string solution(string bin1, string bin2) {
string result = "";
int carry = 0;
int i = bin1.length() - 1;
int j = bin2.length() - 1;

while(i >= 0 || j >= 0 || carry) {
int sum = carry;
if(i >= 0) sum += bin1[i--] - '0';
if(j >= 0) sum += bin2[j--] - '0';

result += to_string(sum % 2);
carry = sum / 2;
}

reverse(result.begin(), result.end());
return result;
}

0 comments on commit 4802f56

Please sign in to comment.