#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef string str; typedef unsigned long long ull; #define pb push_back #define mp make_pair string addition(string a, string b) { while(a.size() < b.size()) a = '0' + a; while(b.size() < a.size()) b = '0' + b; int carry = 0, sum = 0; string res = ""; for(int i = a.size() - 1; i >= 0; --i) { sum = carry + int(a[i] - '0') + int(b[i] - '0'); if(sum > 9) { res = char((sum % 10) + '0') + res; carry = 1; } else { res = char((sum % 10) + '0') + res; carry = 0; } } if(carry == 1) res = '1' + res; return res; } int main(){ string a, b; cin >> a >> b; cout << addition(a, b); }
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef string str; typedef unsigned long long ull; #define pb push_back #define mp make_pair string addition(string a, string b) { while(a.size() < b.size()) a = '0' + a; while(b.size() < a.size()) b = '0' + b; int carry = 0, sum = 0; string res = ""; for(int i = a.size() - 1; i >= 0; --i) { sum = carry + int(a[i] - '0') + int(b[i] - '0'); if(sum > 9) { res = char((sum % 10) + '0') + res; carry = 1; } else { res = char((sum % 10) + '0') + res; carry = 0; } } if(carry == 1) res = '1' + res; return res; } int main(){ string a, b; cin >> a >> b; cout << addition(a, b); } |