Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace _02.CoreTasks1
- {
- class Program
- {
- static void Main(string[] args)
- {
- //number of digits in the two arrays
- string[] numsString = Console.ReadLine().Split(' ');
- string[] firstString = Console.ReadLine().Split(' ');
- string[] secondString = Console.ReadLine().Split(' ');
- //convert input to an int array
- int[] nums = new int[2];
- for (int i = 0; i < 2; i++)
- {
- nums[i] = int.Parse(numsString[i]);
- }
- // populate first array
- int[] first = new int[nums[0]];
- for (int i = 0; i < nums[0]; i++)
- {
- first[i] = int.Parse(firstString[i]);
- }
- //populate second array
- int[] second = new int[nums[1]];
- for (int i = 0; i < nums[1]; i++)
- {
- second[i] = int.Parse(secondString[i]);
- }
- //compare the two arrays's length
- bool firstIsBigger = false;
- if (first.Length >= second.Length)
- {
- firstIsBigger = true;
- }
- if (firstIsBigger)
- {
- //new array for the final result
- int[] final = new int[first.Length];
- int sum = 0;
- int add = 0;
- for (int i = 0; i < second.Length; i++)
- {
- sum = first[i] + second[i] + add;
- add = 0;
- if (sum > 9)
- {
- add = 1;
- final[i] = sum % 10;
- sum = 0;
- }
- else
- {
- final[i] = sum;
- sum = 0;
- }
- }
- for (int i = second.Length; i < first.Length; i++)
- {
- sum = first[i] + add;
- add = 0;
- if (sum > 9)
- {
- add = 1;
- final[i] = sum % 10;
- sum = 0;
- }
- else
- {
- final[i] = sum;
- sum = 0;
- }
- }
- Console.WriteLine(string.Join(" ", final));
- }
- else
- {
- //new array for the final result
- int[] final = new int[second.Length];
- int sum = 0;
- int add = 0;
- for (int i = 0; i < first.Length; i++)
- {
- sum = first[i] + second[i] + add;
- add = 0;
- if (sum > 9)
- {
- add = 1;
- final[i] = sum % 10;
- sum = 0;
- }
- else
- {
- final[i] = sum;
- sum = 0;
- }
- }
- for (int i = first.Length; i < second.Length; i++)
- {
- sum = second[i] + add;
- add = 0;
- if (sum > 9)
- {
- add = 1;
- final[i] = sum % 10;
- sum = 0;
- }
- else
- {
- final[i] = sum;
- sum = 0;
- }
- }
- Console.WriteLine(string.Join(" ", final));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement