#include <bits/stdc++.h> using namespace std; template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; } template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) { out << "{"; bool fi = 1; for(auto b : a) { if(fi) {out<<b; fi=0;} else out<<", "<<b; } return out << "}"; } void dump(){ cerr << "\e"<<"\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "\e"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) //#define debug(...) ; string add(string a, string b){ a = "0"+a; b = "0"+b; if(a.size() < b.size()) swap(a,b); int cnt = a.size()-b.size(); string buf=""; while(cnt--) buf+="0"; b = buf+b; for(int i=a.size()-1; i>=0; --i){ if(i>=b.size()) continue; a[i] += b[i]-'0'; if(a[i]>'9'){ a[i]-=10; a[i-1]++; } } while(a[0]=='0' && a.size()>1) a.erase(a.begin()); return a; } int main(){ ios_base::sync_with_stdio(0); string a,b; cin >> a >> b; cout << add(a,b); return 0; }
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 | #include <bits/stdc++.h> using namespace std; template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; } template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) { out << "{"; bool fi = 1; for(auto b : a) { if(fi) {out<<b; fi=0;} else out<<", "<<b; } return out << "}"; } void dump(){ cerr << "\e"<<"\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "\e"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) //#define debug(...) ; string add(string a, string b){ a = "0"+a; b = "0"+b; if(a.size() < b.size()) swap(a,b); int cnt = a.size()-b.size(); string buf=""; while(cnt--) buf+="0"; b = buf+b; for(int i=a.size()-1; i>=0; --i){ if(i>=b.size()) continue; a[i] += b[i]-'0'; if(a[i]>'9'){ a[i]-=10; a[i-1]++; } } while(a[0]=='0' && a.size()>1) a.erase(a.begin()); return a; } int main(){ ios_base::sync_with_stdio(0); string a,b; cin >> a >> b; cout << add(a,b); return 0; } |