Advertisement
Guest User

Untitled

a guest
Nov 21st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class AddIntegersAsArrays
  5. {
  6.  
  7. static void Main(string[] args)
  8. {
  9.  
  10. string inputNumDigits = Console.ReadLine();
  11. string[] split = inputNumDigits.Split(' ');
  12. int[] array = new int[2];
  13.  
  14. for (int i = 0; i < split.Length; i++)
  15. {
  16. array[i] = int.Parse(split[i]);
  17. }
  18.  
  19. string inputNum1 = Console.ReadLine();
  20. string[] split1 = inputNum1.Split(' ');
  21. int[] array1 = new int[array[0]];
  22. for (int i = 0; i < array1.Length; i++)
  23. {
  24. array1[i] = int.Parse(split1[i]);
  25. }
  26.  
  27.  
  28. string inputNum2 = Console.ReadLine();
  29. string[] split2 = inputNum2.Split(' ');
  30. int[] array2 = new int[array[1]];
  31. for (int i = 0; i < array2.Length; i++)
  32. {
  33. array2[i] = int.Parse(split2[i]);
  34. }
  35.  
  36.  
  37. int size1 = Math.Min(array1.Length, array2.Length);
  38. int size2 = Math.Max(array1.Length, array2.Length);
  39. SumTwoNumbersAsArray(size1, size2, array1, array2);
  40.  
  41. }
  42. static void SumTwoNumbersAsArray(int size1, int size2, int[] array1, int[] array2)
  43. {
  44. StringBuilder sum = new StringBuilder();
  45. int increaseWithOne = 0;
  46. for (int i = 0; i <= size1 - 1; i++)
  47. {
  48.  
  49.  
  50. sum.Append(((array1[i] + array2[i] + increaseWithOne) % 10) + " ");
  51.  
  52. if ((array1[i] + array2[i] + increaseWithOne) > 9)
  53. {
  54. increaseWithOne = 1;
  55. }
  56. else
  57. {
  58. increaseWithOne = 0;
  59. }
  60. }
  61.  
  62. for (int i = size1; i < size2; i++)
  63. {
  64. if (i < size2 - 1)
  65. {
  66. if (array1.Length > array2.Length)
  67. {
  68. sum.Append((array1[i] + increaseWithOne) + " ");
  69. }
  70. else if (array2.Length > array1.Length)
  71. {
  72. sum.Append((array2[i] + increaseWithOne) + " ");
  73. }
  74. }
  75.  
  76. if (i == size2 - 1)
  77. {
  78. if (array1.Length > array2.Length)
  79. {
  80. sum.Append((array1[i] + increaseWithOne) % 10 + " ");
  81. }
  82. else if (array2.Length > array1.Length)
  83. {
  84. sum.Append((array2[i] + increaseWithOne) % 10 + " ");
  85. }
  86. }
  87.  
  88. }
  89. Console.WriteLine(sum);
  90. }
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement