-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha +b adder
55 lines (52 loc) · 1.02 KB
/
a +b adder
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
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
string str1, str2;
while (cin >> str1 &&cin >> str2)
{
int length1 = str1.size();
int length2 = str2.size();
int min_length = length1 < length2 ? length1 : length2;
string str3;
int cnt = 0;
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
for (int i = 0;i < min_length;++i)
{
int a = str1[i] - '0' + str2[i] - '0' + cnt;
char c = a % 10 + 48;
str3 += c;
cnt = a / 10;
}
if (length1 < length2)
{
for (int j = min_length;j < length2;++j)
{
int a = str2[j] - '0' + cnt;
char c = a % 10 + 48;
str3 += c;
cnt = a / 10;
}
}
else
{
for (int j = min_length;j < length1;++j)
{
int a = str1[j] - '0' + cnt;
char c = a % 10 + 48;
str3 += c;
cnt = a / 10;
}
}
if (cnt == 1)
str3 += '1';
for (auto ix = str3.crbegin();ix != str3.crend();++ix)
cout << *ix;
cout << endl;
}
return 0;
}
//a + b累加器, a 和 b位数不超过1000位