fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAX_SIZE = 1000;
  5. const int TEN = 10;
  6.  
  7. void add(int a[], int b[], int sum[]) {
  8. int maxLength = a[0];
  9. if (b[0] > maxLength) {
  10. maxLength = b[0];
  11. }
  12. int carry = 0;
  13. int sumLength = 0;
  14. for (int i = 1; i <= maxLength; ++i) {
  15. int digitA = 0;
  16. int digitB = 0;
  17. if (i <= a[0]) {
  18. digitA = a[i];
  19. }
  20. if (i <= b[0]) {
  21. digitB = b[i];
  22. }
  23. int currentSum = digitA + digitB + carry;
  24. sum[++sumLength] = currentSum % 10;
  25. carry = currentSum / 10;
  26. }
  27. if (carry > 0) {
  28. sum[++sumLength] = carry;
  29. }
  30. sum[0] = sumLength;
  31. cout << sum[0] << " ";
  32. for (int i = 1; i <= sumLength; ++i) {
  33. cout << sum[i] << " ";
  34. }
  35. }
  36. /*
  37. void add(int a[], int b[], int rezultat[]) {
  38. for (int i = 1; i <= a[0] || i <= b[0]; ++i) {
  39. int sum = rezultat[i] + a[i] + b[i];
  40. rezultat[i] = sum % TEN;
  41. rezultat[i + 1] = sum / TEN;
  42. rezultat[0] = i;
  43. if (sum >= TEN) {
  44. rezultat[0] = i + 1;
  45. }
  46. }
  47. }
  48. */
  49. int main() {
  50. const int MAX_SIZE = 1000;
  51. int a[MAX_SIZE + 1], b[MAX_SIZE + 1], rezultat[MAX_SIZE + 1] = {0};
  52. cin >> a[0];
  53. for (int i = 1; i <= a[0]; ++i) {
  54. cin >> a[i];
  55. }
  56. cin >> b[0];
  57. for (int i = 1; i <= b[0]; ++i) {
  58. cin >> b[i];
  59. }
  60. add(a, b, rezultat);
  61. for (int i = 0; i <= rezultat[0]; ++i) {
  62. cout << rezultat[i] << " ";
  63. }
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0.01s 5268KB
stdin
2 4 3 1 9
stdout
2 3 4 2 3 4