Advertisement
vlad0

Methods 8

Jan 14th, 2013
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _08.SumTwoArraysOfDigits
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             DateTime start = DateTime.Now;
  13.             byte[] firstArray = { 9, 4, 1, 9 };
  14.             byte[] secondArray = { 1, 5, 6, 7, 9, 9, 9 };
  15.  
  16.             string total = SumArrays(firstArray, secondArray);
  17.             Console.WriteLine(total);
  18.             DateTime final = DateTime.Now;
  19.             Console.WriteLine("{0}", final - start);
  20.  
  21.         }
  22.  
  23.         private static string SumArrays(byte[] firstArray, byte[] secondArray)
  24.         {
  25.             List<byte> maxArray = new List<byte>();
  26.             List<byte> minArray = new List<byte>();
  27.             if (firstArray.Length>secondArray.Length)
  28.             {
  29.                 maxArray.AddRange(firstArray);
  30.                 minArray.AddRange(secondArray);
  31.             }
  32.             else
  33.             {
  34.                 maxArray.AddRange(secondArray);
  35.                 minArray.AddRange(firstArray);
  36.             }
  37.  
  38.             int minLength = minArray.Count;
  39.             int maxLength = maxArray.Count;
  40.             int addition = 0;
  41.             int sum;
  42.             StringBuilder result = new StringBuilder();
  43.             for (int i = 0; i < minLength; i++)
  44.             {
  45.                 sum = minArray[i] + maxArray[i] + addition;
  46.                 if (sum>=10)
  47.                 {
  48.                     addition = 1;
  49.                     sum = sum % 10;
  50.                     result.Append(sum);
  51.                 }
  52.                 else
  53.                 {
  54.                     result.Append(sum);
  55.                     addition = 0;
  56.                 }
  57.             }
  58.  
  59.             for (int j = minLength; j < maxLength; j++)
  60.             {
  61.                 sum =maxArray[j] + addition;
  62.                 if (sum >= 10)
  63.                 {
  64.                     addition = 1;
  65.                     sum = sum % 10;
  66.                     result.Append(sum);
  67.                 }
  68.                 else
  69.                 {
  70.                     result.Append(sum);
  71.                     addition = 0;
  72.                 }
  73.             }
  74.             if (addition==1)
  75.             {
  76.                 result.Append(1);
  77.             }
  78.             char[] reversed = (result.ToString()).ToCharArray();
  79.             result.Clear();
  80.             for (int i = reversed.Length-1; i >=0; i--)
  81.             {
  82.                 result = result.Append(reversed[i]);
  83.             }
  84.             return result.ToString();
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement