Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- class AddIntegersAsArrays
- {
- static void Main(string[] args)
- {
- string inputNumDigits = Console.ReadLine();
- string[] split = inputNumDigits.Split(' ');
- int[] array = new int[2];
- for (int i = 0; i < split.Length; i++)
- {
- array[i] = int.Parse(split[i]);
- }
- string inputNum1 = Console.ReadLine();
- string[] split1 = inputNum1.Split(' ');
- int[] array1 = new int[array[0]];
- for (int i = 0; i < array1.Length; i++)
- {
- array1[i] = int.Parse(split1[i]);
- }
- string inputNum2 = Console.ReadLine();
- string[] split2 = inputNum2.Split(' ');
- int[] array2 = new int[array[1]];
- for (int i = 0; i < array2.Length; i++)
- {
- array2[i] = int.Parse(split2[i]);
- }
- int size1 = Math.Min(array1.Length, array2.Length);
- int size2 = Math.Max(array1.Length, array2.Length);
- SumTwoNumbersAsArray(size1, size2, array1, array2);
- }
- static void SumTwoNumbersAsArray(int size1, int size2, int[] array1, int[] array2)
- {
- StringBuilder sum = new StringBuilder();
- int increaseWithOne = 0;
- for (int i = 0; i <= size1 - 1; i++)
- {
- sum.Append(((array1[i] + array2[i] + increaseWithOne) % 10) + " ");
- if ((array1[i] + array2[i] + increaseWithOne) > 9)
- {
- increaseWithOne = 1;
- }
- else
- {
- increaseWithOne = 0;
- }
- }
- for (int i = size1; i < size2; i++)
- {
- if (i < size2 - 1)
- {
- if (array1.Length > array2.Length)
- {
- sum.Append((array1[i] + increaseWithOne) + " ");
- }
- else if (array2.Length > array1.Length)
- {
- sum.Append((array2[i] + increaseWithOne) + " ");
- }
- }
- if (i == size2 - 1)
- {
- if (array1.Length > array2.Length)
- {
- sum.Append((array1[i] + increaseWithOne) % 10 + " ");
- }
- else if (array2.Length > array1.Length)
- {
- sum.Append((array2[i] + increaseWithOne) % 10 + " ");
- }
- }
- }
- Console.WriteLine(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement