-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1252.cpp
93 lines (85 loc) · 1.19 KB
/
1252.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 1252. 이진수 덧셈
// 2019.09.02
// 문자열 처리, 자료구조, 스택
#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
using namespace std;
int main()
{
string a;
string b;
cin >> a >> b;
stack<int> s1;
stack<int> s2;
stack<int> s3;
for (int i = 0; i < a.size(); i++)
{
s1.push(a[i] - '0');
}
for (int i = 0; i < b.size(); i++)
{
s2.push(b[i] - '0');
}
int carry = 0;
while (1)
{
if (s1.empty() && s2.empty())
{
// 마지막에 carry가 있다면 1추가
if (carry == 1)
{
s3.push(1);
}
break;
}
int x = 0;
int y = 0;
if (!s1.empty())
{
x = s1.top();
s1.pop();
}
if (!s2.empty())
{
y = s2.top();
s2.pop();
}
if (x + y + carry >= 2)
{
s3.push(x + y + carry - 2);
carry = 1;
}
else
{
s3.push(x + y + carry);
carry = 0;
}
}
bool flag = false; // 0000000 + 0000000 일때 0을 출력하기 위한 플래그
while (!s3.empty())
{
if (s3.top() == 0 && !flag)
{
s3.pop();
}
else if (s3.top() == 1 && !flag)
{
cout << s3.top();
s3.pop();
flag = 1;
}
else
{
cout << s3.top();
s3.pop();
}
}
if (!flag)
{
cout << 0;
}
cout << endl;
return 0;
}