#include<bits/stdc++.h> #define fast ios_base::sync_with_stdio(0);cin.tie(0); #define ll long long using namespace std; string suma(string str1, string str2) { if (str1.length() > str2.length()) swap(str1, str2); string str = ""; ll n1 = str1.length(), n2 = str2.length(); ll diff = n2 - n1; ll num = 0; for (int i=n1-1; i>=0; i--) { ll sum = ((str1[i]-'0') + (str2[i+diff]-'0') + num); str.push_back(sum%10 + '0'); num = sum/10; } for (int i=n2-n1-1; i>=0; i--) { ll sum = ((str2[i]-'0')+num); str.push_back(sum%10 + '0'); num = sum/10; } if (num) str.push_back(num+'0'); reverse(str.begin(), str.end()); return str; } int main() { fast; string l1, l2; cin>>l1>>l2; cout << suma(l1, l2); 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 | #include<bits/stdc++.h> #define fast ios_base::sync_with_stdio(0);cin.tie(0); #define ll long long using namespace std; string suma(string str1, string str2) { if (str1.length() > str2.length()) swap(str1, str2); string str = ""; ll n1 = str1.length(), n2 = str2.length(); ll diff = n2 - n1; ll num = 0; for (int i=n1-1; i>=0; i--) { ll sum = ((str1[i]-'0') + (str2[i+diff]-'0') + num); str.push_back(sum%10 + '0'); num = sum/10; } for (int i=n2-n1-1; i>=0; i--) { ll sum = ((str2[i]-'0')+num); str.push_back(sum%10 + '0'); num = sum/10; } if (num) str.push_back(num+'0'); reverse(str.begin(), str.end()); return str; } int main() { fast; string l1, l2; cin>>l1>>l2; cout << suma(l1, l2); return 0; } |