encho253

Untitled

Nov 17th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 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 arrayAsString = Console.ReadLine();
  30.         int[] intArray = new int[size];
  31.  
  32.         intArray = arrayAsString.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.                 if (sum > 9)
  74.                 {
  75.                     sum %= 10;
  76.                     keepInMind = 1;
  77.                 }
  78.                 else
  79.                 {
  80.                     keepInMind = 0;
  81.                 }
  82.                 sumOfTwoArrays.Add(sum);
  83.             }
  84.         }
  85.         return sumOfTwoArrays;          
  86.     }
  87.  
  88.     /// <summary>
  89.     /// Whoes is the smallest array.
  90.     /// </summary>
  91.     /// <param name="firstArray">The first array.</param>
  92.     /// <param name="secondArray">The second array.</param>
  93.     /// <returns></returns>
  94.     static KeyValuePair<int[], int[]> WhoIsTheSmallestArray(int[] firstArray, int[] secondArray)
  95.     {
  96.         if (firstArray.Length > secondArray.Length)
  97.         {
  98.             return new KeyValuePair<int[], int[]>(firstArray,secondArray);
  99.         }
  100.         else if (firstArray.Length < secondArray.Length)
  101.         {
  102.             return new KeyValuePair<int[], int[]>(secondArray, firstArray);
  103.         }
  104.         else
  105.         {
  106.             return new KeyValuePair<int[], int[]>(secondArray, firstArray);
  107.         }
  108.     }
  109.     /// <summary>
  110.     /// Prints the sum of the arrays.
  111.     /// </summary>
  112.     /// <param name="sumOfTwoArrays">The sum of two arrays.</param>
  113.     static void PrintSumOfTheArrays(List<int> sumOfTwoArrays)
  114.     {
  115.         foreach (var item in sumOfTwoArrays)
  116.         {
  117.             Console.Write(item + " ");
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment