encho253

Untitled

Nov 16th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. /// <summary>
  6. /// Write a method that adds two positive integer numbers represented as arrays of digits
  7. /// (each array element arr[i] contains a digit; the last digit is kept in arr[0]).
  8. /// Write a program that reads two arrays representing positive integers and outputs their sum.
  9. /// </summary>
  10. class NumberAsArray
  11. {
  12.     /// <summary>
  13.     /// Defines the entry point of the application.
  14.     /// </summary>
  15.     static void Main()
  16.     {
  17.         string size = Console.ReadLine();
  18.         int[] sizeArray = size.Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
  19.         PrintSumOfTheArrays(SumOfTwoArrays(InputArrayAsString(sizeArray[0]), InputArrayAsString(sizeArray[1])));      
  20.     }
  21.  
  22.     /// <summary>
  23.     /// Inputs the array as string.
  24.     /// </summary>
  25.     /// <param name="size">The size.</param>
  26.     /// <returns></returns>
  27.     static int[] InputArrayAsString(int size)
  28.     {                
  29.         string firstString = Console.ReadLine();
  30.  
  31.         int[] intArray = new int[size];
  32.         intArray = firstString.Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
  33.  
  34.         return intArray;
  35.     }
  36.  
  37.     /// <summary>
  38.     /// Sums of two arrays.
  39.     /// </summary>
  40.     /// <param name="firstArray">The first array.</param>
  41.     /// <param name="secondArray">The second array.</param>
  42.     /// <returns></returns>
  43.     static List<int> SumOfTwoArrays(int[] firstArray,int[] secondArray)
  44.     {
  45.         List<int> sumOfTwoArrays = new List<int>();
  46.         int sum = 0;
  47.         int keepInMind = 0;
  48.  
  49.         KeyValuePair<int[],int[]> pair = WhoIsTheSmallestArray(firstArray,secondArray);
  50.         int[] smallestArray = pair.Value;
  51.         int[] bigestArray = pair.Key;
  52.  
  53.         for (int i = 0; i < bigestArray.Length ; i++)
  54.         {
  55.             if (i < smallestArray.Length)
  56.             {
  57.                 if (smallestArray[i] + bigestArray[i] + keepInMind > 9)
  58.                 {
  59.                     sum = (smallestArray[i] + bigestArray[i] + keepInMind) % 10;
  60.                     sumOfTwoArrays.Add(sum);
  61.                     keepInMind = 1;
  62.                 }
  63.                 else
  64.                 {
  65.                     sum = smallestArray[i] + bigestArray[i] + keepInMind;
  66.                     sumOfTwoArrays.Add(sum);
  67.                     keepInMind = 0;
  68.                 }
  69.             }
  70.             else
  71.             {
  72.                 sum = bigestArray[i] + keepInMind + 0;
  73.                 sumOfTwoArrays.Add(sum);
  74.                 keepInMind = 0;
  75.             }
  76.         }
  77.         return sumOfTwoArrays;          
  78.     }
  79.  
  80.     /// <summary>
  81.     /// Whoes is the smallest array.
  82.     /// </summary>
  83.     /// <param name="firstArray">The first array.</param>
  84.     /// <param name="secondArray">The second array.</param>
  85.     /// <returns></returns>
  86.     static KeyValuePair<int[], int[]> WhoIsTheSmallestArray(int[] firstArray, int[] secondArray)
  87.     {
  88.         if (firstArray.Length > secondArray.Length)
  89.         {
  90.             return new KeyValuePair<int[], int[]>(firstArray,secondArray);
  91.         }
  92.         else if (firstArray.Length < secondArray.Length)
  93.         {
  94.             return new KeyValuePair<int[], int[]>(secondArray, firstArray);
  95.         }
  96.         else
  97.         {
  98.             return new KeyValuePair<int[], int[]>(secondArray, firstArray);
  99.         }
  100.     }
  101.     /// <summary>
  102.     /// Prints the sum of the arrays.
  103.     /// </summary>
  104.     /// <param name="sumOfTwoArrays">The sum of two arrays.</param>
  105.     static void PrintSumOfTheArrays(List<int> sumOfTwoArrays)
  106.     {
  107.         foreach (var item in sumOfTwoArrays)
  108.         {
  109.             Console.Write(item + " ");
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment