#include <bits/stdc++.h> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef LOCAL #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif #define ar array #define ll long long #define ld long double #define sza(x) ((int)x.size()) #define all(a) (a).begin(), (a).end() const int MAX_N = 1e5 + 5; const ll MOD = 1e9 + 7; const ll INF = 1e9; const ld EPS = 1e-9; string solve(string a, string b) { if(a.length()>b.length()) swap(a,b); string result = ""; int n1 = a.length(); int n2 = b.length(); reverse(a.begin(),a.end()); reverse(b.begin(),b.end()); int carry =0; for(int i=0; i<n1;i++){ int sum = ((a[i]-48)+(b[i]-48)+carry); result.push_back(sum%10 + 48); carry = sum/10; } for(int i=n1; i<n2; i++){ int sum =((b[i]-48) + carry); result.push_back(sum%10+48); carry = sum/10; } if(carry) result.push_back(carry+48); reverse(result.begin(),result.end()); return result; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string a; string b; int tc = 1; // cin >> tc; for (int t = 1; t <= tc; t++) { cin >> a; cin >> b; cout << solve(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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #include <bits/stdc++.h> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef LOCAL #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif #define ar array #define ll long long #define ld long double #define sza(x) ((int)x.size()) #define all(a) (a).begin(), (a).end() const int MAX_N = 1e5 + 5; const ll MOD = 1e9 + 7; const ll INF = 1e9; const ld EPS = 1e-9; string solve(string a, string b) { if(a.length()>b.length()) swap(a,b); string result = ""; int n1 = a.length(); int n2 = b.length(); reverse(a.begin(),a.end()); reverse(b.begin(),b.end()); int carry =0; for(int i=0; i<n1;i++){ int sum = ((a[i]-48)+(b[i]-48)+carry); result.push_back(sum%10 + 48); carry = sum/10; } for(int i=n1; i<n2; i++){ int sum =((b[i]-48) + carry); result.push_back(sum%10+48); carry = sum/10; } if(carry) result.push_back(carry+48); reverse(result.begin(),result.end()); return result; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string a; string b; int tc = 1; // cin >> tc; for (int t = 1; t <= tc; t++) { cin >> a; cin >> b; cout << solve(a,b); } return 0; } |