Advertisement
Guest User

Untitled

a guest
Oct 26th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _06.Sum_big_numbers
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string input1 = Console.ReadLine();
  14.             string input2 = Console.ReadLine();
  15.             string shortestNum = DetermineShortestNum(input1, input2);
  16.             string longestNum = DetermineLongestNum(input1, input2);
  17.             int sum = 0;
  18.             int count = 0;
  19.             List<string> sumString = new List<string>();
  20.  
  21.             for (int i = 0; i < shortestNum.Length; i++)
  22.             {                
  23.                 sum = (Convert.ToInt32(shortestNum[shortestNum.Length - 1 - i].ToString()) + Convert.ToInt32(longestNum[longestNum.Length - 1 - i].ToString()) + count);
  24.                 count = 0;
  25.                 if (sum >= 10)
  26.                 {
  27.                     sum %= 10;
  28.                     count++;
  29.                 }
  30.                 sumString.Add(sum.ToString());
  31.             }
  32.             if (count != 0)
  33.             {
  34.                 int dif = longestNum.Length - shortestNum.Length;
  35.                 int lastCount = Convert.ToInt32(longestNum[longestNum.Last()].ToString()) + count;
  36.                 sumString.Add(lastCount.ToString());
  37.                 if (shortestNum.Length != longestNum.Length)
  38.                 {                    
  39.                     sumString.Add(new string(longestNum.Take(dif - 1).ToArray()));
  40.                 }
  41.             }
  42.             else
  43.             {
  44.                 if (shortestNum.Length != longestNum.Length)
  45.                 {
  46.                     int dif = longestNum.Length - shortestNum.Length;
  47.                     sumString.Add(new string(longestNum.Take(dif).ToArray()));
  48.                 }
  49.             }
  50.            
  51.             sumString.Reverse();
  52.             Console.WriteLine(string.Join("", sumString));
  53.            
  54.         }
  55.  
  56.         static string DetermineShortestNum(string num1, string num2)
  57.         {
  58.             if (num1.Length != num2.Length)
  59.             {
  60.                 if (num1.Length == Math.Min(num1.Length, num2.Length))
  61.                 {
  62.                     return num1;
  63.                 }
  64.                 else
  65.                 {
  66.                     return num2;
  67.                 }
  68.             }
  69.             else
  70.             {
  71.                 return num1;
  72.             }
  73.            
  74.         }
  75.  
  76.         static string DetermineLongestNum(string num1, string num2)
  77.         {
  78.             if (num1.Length != num2.Length)
  79.             {
  80.                 if (num1.Length == Math.Max(num1.Length, num2.Length))
  81.                 {
  82.                     return num1;
  83.                 }
  84.                 else
  85.                 {
  86.                     return num2;
  87.                 }
  88.             }
  89.             else
  90.             {
  91.                 return num2;
  92.             }
  93.            
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement