-
Notifications
You must be signed in to change notification settings - Fork 0
/
큰 수 A+B.cc
51 lines (40 loc) · 1016 Bytes
/
큰 수 A+B.cc
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
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string A, B, C = "";
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> A >> B;
int len_A = A.length(), len_B = B.length();
int carry = 0;
string err = "";
for (int i = 0; i < abs(len_A - len_B); i++) {
err += '0';
}
if (len_A > len_B) {
B = err + B;
len_B = len_A;
}
else if (len_A < len_B) {
A = err + A;
len_A = len_B;
}
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
for (int i = 0; i < len_A; i++) {
if (A[i] - '0' + B[i] - '0' + carry >= 10) {
C += (A[i] - '0' + B[i] - '0' + carry) % 10 + '0';
carry = 1;
}
else {
C += (A[i] - '0' + B[i] + carry);
carry = 0;
}
}
reverse(C.begin(), C.end());
if (carry == 1) C = '1' + C;
cout << C;
return 0;
}