Algo-Trian
This commit is contained in:
35
Algorithm/BigNum/P1601 A+B Problem(高精).cpp
Normal file
35
Algorithm/BigNum/P1601 A+B Problem(高精).cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
string add(string a, string b) {
|
||||
reverse(a.begin(), a.end());
|
||||
reverse(b.begin(), b.end());
|
||||
|
||||
int ms = max(a.size(), b.size());
|
||||
string tmp;
|
||||
tmp.resize(ms + 1, '0');
|
||||
|
||||
int carry = 0;
|
||||
for (int i = 0; i < ms; i++) {
|
||||
int x = (i < a.size() ? a[i] - '0' : 0);
|
||||
int y = (i < b.size() ? b[i] - '0' : 0);
|
||||
int sum = x + y + carry;
|
||||
tmp[i] = sum % 10 + '0';
|
||||
carry = sum / 10;
|
||||
}
|
||||
if (carry) tmp[ms] = carry + '0';
|
||||
else tmp.pop_back();
|
||||
|
||||
reverse(tmp.begin(), tmp.end());
|
||||
return tmp;
|
||||
}
|
||||
|
||||
int main() {
|
||||
string a, b;
|
||||
cin >> a >> b;
|
||||
cout << add(a, b) << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user