#include<bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define sz(x) (int)x.size() #define all(x) x.begin(),x.end() #define sor(x) sort(all(x)) #define rev(x) reverse(all(x)) #define rsz(x) resize(x) #define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef long long ll; typedef pair<int,int>pii; typedef long double ldl; typedef pair<ll,ll>pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vii; typedef vector<pll>vll; typedef unsigned long long ul; int main() { BOOST string a,b; cin>>a>>b; int s = max(sz(a),sz(b))-min(sz(a),sz(b)); string t(s,'0'); if(sz(a) < sz(b)) a = t+a; else b = t+b; int r =0; string ans =""; for(int i = sz(a)-1 ; i >=0 ; i--) { ans+=char((a[i]+b[i]+r-2*'0')%10 + '0'); r = (a[i]+b[i]+r-2*'0')/10; } if(r) ans+=char(r+'0'); rev(ans); cout<<ans; 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 | #include<bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define sz(x) (int)x.size() #define all(x) x.begin(),x.end() #define sor(x) sort(all(x)) #define rev(x) reverse(all(x)) #define rsz(x) resize(x) #define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef long long ll; typedef pair<int,int>pii; typedef long double ldl; typedef pair<ll,ll>pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vii; typedef vector<pll>vll; typedef unsigned long long ul; int main() { BOOST string a,b; cin>>a>>b; int s = max(sz(a),sz(b))-min(sz(a),sz(b)); string t(s,'0'); if(sz(a) < sz(b)) a = t+a; else b = t+b; int r =0; string ans =""; for(int i = sz(a)-1 ; i >=0 ; i--) { ans+=char((a[i]+b[i]+r-2*'0')%10 + '0'); r = (a[i]+b[i]+r-2*'0')/10; } if(r) ans+=char(r+'0'); rev(ans); cout<<ans; return 0; } |