#include <bits/stdc++.h> using namespace std; int *result; bool is_move; void add(string a, string b) { int a_n = a.size(); int b_n = b.size(); if (b_n > a_n) { swap(a_n, b_n); swap(a, b); } // a is longer string new_b = ""; int diff = a_n - b_n; while (diff > 0) { new_b += "0"; diff--; } new_b += b; b = new_b; b_n = a_n; // a and b are the same length int add_a, add_b, sum = 0; for (int i = a_n - 1; i >= 0; i--) { add_a = a[i] - '0'; add_b = b[i] - '0'; sum = (add_a + add_b); if (is_move == true) sum++; if (sum > 9) { is_move = true; sum = sum % 10; } else { is_move = false; } result[i] = sum; } } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); string a, b; cin >> a >> b; int max_n = max(a.size(), b.size()); result = new int[max_n]; is_move = false; add(a, b); if (is_move) cout << 1; for (int i = 0; i < max_n; i++) { cout << result[i]; } 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 | #include <bits/stdc++.h> using namespace std; int *result; bool is_move; void add(string a, string b) { int a_n = a.size(); int b_n = b.size(); if (b_n > a_n) { swap(a_n, b_n); swap(a, b); } // a is longer string new_b = ""; int diff = a_n - b_n; while (diff > 0) { new_b += "0"; diff--; } new_b += b; b = new_b; b_n = a_n; // a and b are the same length int add_a, add_b, sum = 0; for (int i = a_n - 1; i >= 0; i--) { add_a = a[i] - '0'; add_b = b[i] - '0'; sum = (add_a + add_b); if (is_move == true) sum++; if (sum > 9) { is_move = true; sum = sum % 10; } else { is_move = false; } result[i] = sum; } } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); string a, b; cin >> a >> b; int max_n = max(a.size(), b.size()); result = new int[max_n]; is_move = false; add(a, b); if (is_move) cout << 1; for (int i = 0; i < max_n; i++) { cout << result[i]; } return 0; } |