-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path0078.cpp
More file actions
41 lines (38 loc) · 796 Bytes
/
0078.cpp
File metadata and controls
41 lines (38 loc) · 796 Bytes
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
// 0078.超长正整数相加
// keywords: 模拟加法
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string add(string a, string b)
{
string r = "";
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
if(a.size() < b.size())
{
a.append(b.size()-a.size(), '0');
} else if(a.size() > b.size())
{
b.append(a.size()-b.size(), '0');
}
int c = 0;
for(int i = 0; i < a.size(); i++)
{
int t = a[i] + b[i] - '0'*2 + c;
r.append(1, t % 10+'0');
c = t / 10;
}
if(c) r.append(1, c+'0');
reverse(r.begin(), r.end());
return r;
}
int main()
{
string s1, s2;
while(getline(cin, s1))
{
getline(cin, s2);
cout << add(s1, s2) << endl;
}
}