-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36add.cpp
75 lines (69 loc) · 1.6 KB
/
36add.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
#include<vector>
#include<string>
#include<iostream>
using namespace std;
string add(const string & lsh, const string & rsh)
{
string res;
int carry = 0, i = lsh.size() - 1, j = rsh.size() - 1;
for(; i >= 0 && j >= 0; --i, --j){
int l = 0, r = 0;
if(lsh[i] >= 'a' && lsh[i] <= 'z'){
l = lsh[i] - 'a' + 10;
}else{
l = lsh[i] - '0';
}
if(rsh[j] >= 'a' && rsh[j] <= 'z'){
r = rsh[j] - 'a' + 10;
}else{
r = rsh[j] - '0';
}
int sum = l + r + carry;
carry = sum / 36;
char c = (sum % 36) <= 9 ? (sum % 36) + '0' : (sum % 36) - 10 + 'a';
res.push_back(c);
}
while (i >= 0)
{
int sum = lsh[i] + carry;
char c;
if(sum == '9' + 1){
carry = 0;
c = 'a';
}else if (sum == 'z' + 1){
carry = 1;
c = '0';
}else{
carry = 0;
c = sum;
}
res.push_back(c);
cout << "res: " << res << "c: " << c << endl;
--i;
}
while (j >= 0)
{
int sum = rsh[j] + carry;
char c;
if(sum >= '0' && sum <= '9'){
carry = 0;
c = sum;
}else if(sum == '9' + 1){
carry = 0;
c = 'a';
}else if (sum == 'z' + 1){
carry = 1;
c = '0';
}
res.push_back(c);
++j;
}
if(carry == 1)
res.push_back('1');
reverse(res.begin(), res.end());
return res;
}
int main()
{
cout << add("abbbb", "1") << endl;
}