안녕하세요. 오늘은 이진수를 더할거예요.
https://www.acmicpc.net/problem/2729
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string nonzero(string s)
{
int len = s.length(), i;
for (i = 0; i < len; i++)
if (s[i] == '1')
return s.substr(i, len - i);
return "0";
}
int main(void)
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
int T;
cin >> T;
while (T--)
{
string s, s2;
int len, len2;
cin >> s >> s2; s = nonzero(s); s2 = nonzero(s2);
len = s.length(), len2 = s2.length();
if (len < len2)
{
swap(s, s2);
swap(len, len2);
}
int arr[101] = { 0 };
for (int i = 0; i < len; i++)
{
int x = s[i] - '0';
if (len - i <= len2) x += s2[i - len + len2] - '0';
arr[i] = x;
}
for (int i = len - 1; i > 0; i--)
{
if (arr[i] >= 2)
{
arr[i - 1]++;
arr[i] -= 2;
}
}
if (arr[0] >= 2) cout << 1;
for (int i = 0; i < len; i++) cout << arr[i] % 2;
cout << "\n";
}
}
감사합니다.