#include<bits/stdc++.h> #define st first #define nd second #define pb(x) push_back(x) #define pp(x) pop_back(x) #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); string A, B; const int MN = 5005; int pier[MN], dru[MN]; int wyn[MN]; stack <int> S; void run(){ cin >> A >> B; for(int i = sz(A)-1; i > -1; i--) pier[sz(A) - 1 - i] = A[i] - '0'; for(int i = sz(B) - 1; i > -1; i--) dru[sz(B) - 1 - i] = B[i] - '0'; int lim = max(sz(A), sz(B)); for(int i = 0; i < lim; i++){ wyn[i] += pier[i] + dru[i]; wyn[i+1] += wyn[i]/10; wyn[i] %= 10; } for(int i = 0; i < lim; i++) S.push(wyn[i]); if(wyn[lim] > 0) S.push(wyn[lim]); while(!S.empty()){ cout << S.top() ; S.pop(); } } int main(){ run(); }
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 | #include<bits/stdc++.h> #define st first #define nd second #define pb(x) push_back(x) #define pp(x) pop_back(x) #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); string A, B; const int MN = 5005; int pier[MN], dru[MN]; int wyn[MN]; stack <int> S; void run(){ cin >> A >> B; for(int i = sz(A)-1; i > -1; i--) pier[sz(A) - 1 - i] = A[i] - '0'; for(int i = sz(B) - 1; i > -1; i--) dru[sz(B) - 1 - i] = B[i] - '0'; int lim = max(sz(A), sz(B)); for(int i = 0; i < lim; i++){ wyn[i] += pier[i] + dru[i]; wyn[i+1] += wyn[i]/10; wyn[i] %= 10; } for(int i = 0; i < lim; i++) S.push(wyn[i]); if(wyn[lim] > 0) S.push(wyn[lim]); while(!S.empty()){ cout << S.top() ; S.pop(); } } int main(){ run(); } |