#include <bits/stdc++.h> using namespace std; void suma(string a, string b){ int l1 = a.length(); int l2 = b.length(); int add=0; int maxi = max(l1, l2); vector<int> v; for(int i=0;i<maxi;i++){ int index1 = (l1-i)-1; int index2 = (l2-i)-1; int c1 = a[index1]-'0'; int c2 = b[index2]-'0'; if(index1 >= 0 && index2 >= 0){ int num = c1+c2+add; v.push_back(num%10); add=num/10; } else{ if(index1 >= 0){ int num = c1+add; v.push_back(num%10); add=num/10; } else{ int num = c2+add; v.push_back(num%10); add=num/10; } } } if(add != 0){ v.push_back(add); } for(int i=v.size()-1;i>=0;i--){ cout << v[i]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string a, b; cin >> a >> b; suma(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 | #include <bits/stdc++.h> using namespace std; void suma(string a, string b){ int l1 = a.length(); int l2 = b.length(); int add=0; int maxi = max(l1, l2); vector<int> v; for(int i=0;i<maxi;i++){ int index1 = (l1-i)-1; int index2 = (l2-i)-1; int c1 = a[index1]-'0'; int c2 = b[index2]-'0'; if(index1 >= 0 && index2 >= 0){ int num = c1+c2+add; v.push_back(num%10); add=num/10; } else{ if(index1 >= 0){ int num = c1+add; v.push_back(num%10); add=num/10; } else{ int num = c2+add; v.push_back(num%10); add=num/10; } } } if(add != 0){ v.push_back(add); } for(int i=v.size()-1;i>=0;i--){ cout << v[i]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string a, b; cin >> a >> b; suma(a, b); return 0; } |