Advertisement
gosharski

BigNumbers

Jan 10th, 2021
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _02.CoreTasks1
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //number of digits in the two arrays
  10. string[] numsString = Console.ReadLine().Split(' ');
  11. string[] firstString = Console.ReadLine().Split(' ');
  12. string[] secondString = Console.ReadLine().Split(' ');
  13.  
  14. //convert input to an int array
  15. int[] nums = new int[2];
  16. for (int i = 0; i < 2; i++)
  17. {
  18. nums[i] = int.Parse(numsString[i]);
  19.  
  20. }
  21.  
  22. // populate first array
  23. int[] first = new int[nums[0]];
  24.  
  25. for (int i = 0; i < nums[0]; i++)
  26. {
  27. first[i] = int.Parse(firstString[i]);
  28.  
  29. }
  30.  
  31. //populate second array
  32. int[] second = new int[nums[1]];
  33.  
  34. for (int i = 0; i < nums[1]; i++)
  35. {
  36. second[i] = int.Parse(secondString[i]);
  37.  
  38. }
  39.  
  40. //compare the two arrays's length
  41. bool firstIsBigger = false;
  42. if (first.Length >= second.Length)
  43. {
  44. firstIsBigger = true;
  45. }
  46.  
  47.  
  48.  
  49.  
  50. if (firstIsBigger)
  51. {
  52. //new array for the final result
  53. int[] final = new int[first.Length];
  54. int sum = 0;
  55. int add = 0;
  56. for (int i = 0; i < second.Length; i++)
  57. {
  58. sum = first[i] + second[i] + add;
  59. add = 0;
  60. if (sum > 9)
  61. {
  62. add = 1;
  63. final[i] = sum % 10;
  64. sum = 0;
  65. }
  66. else
  67. {
  68. final[i] = sum;
  69. sum = 0;
  70. }
  71. }
  72. for (int i = second.Length; i < first.Length; i++)
  73. {
  74. sum = first[i] + add;
  75. add = 0;
  76. if (sum > 9)
  77. {
  78. add = 1;
  79. final[i] = sum % 10;
  80. sum = 0;
  81. }
  82. else
  83. {
  84. final[i] = sum;
  85. sum = 0;
  86. }
  87.  
  88. }
  89. Console.WriteLine(string.Join(" ", final));
  90. }
  91. else
  92. {
  93. //new array for the final result
  94. int[] final = new int[second.Length];
  95. int sum = 0;
  96. int add = 0;
  97. for (int i = 0; i < first.Length; i++)
  98. {
  99. sum = first[i] + second[i] + add;
  100. add = 0;
  101. if (sum > 9)
  102. {
  103. add = 1;
  104. final[i] = sum % 10;
  105. sum = 0;
  106. }
  107. else
  108. {
  109. final[i] = sum;
  110. sum = 0;
  111. }
  112. }
  113. for (int i = first.Length; i < second.Length; i++)
  114. {
  115. sum = second[i] + add;
  116. add = 0;
  117. if (sum > 9)
  118. {
  119. add = 1;
  120. final[i] = sum % 10;
  121. sum = 0;
  122. }
  123. else
  124. {
  125. final[i] = sum;
  126. sum = 0;
  127. }
  128.  
  129. }
  130. Console.WriteLine(string.Join(" ", final));
  131. }
  132.  
  133.  
  134.  
  135. }
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement