JulianJulianov

10.TextProcessingExercise-Multiply Big Number Like A String

Apr 19th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1. 10.Multiply Big Number
  2. You are given two lines – the first one can be a really big number (0 to 10Pow50). The second one will be a single digit number (0 to 9). You must display the product of these numbers.
  3. Note: do not use the BigInteger class.
  4. Examples
  5. Input                                Output
  6. 23                                   46
  7. 2
  8.    
  9. 9999                                 89991
  10. 9
  11.    
  12. 923847238931983192462832102          3695388955727932769851328408
  13. 4
  14.  
  15. using System;
  16. using System.Collections.Generic;
  17.  
  18. public class Program
  19. {
  20.     public static void Main()
  21.     {
  22.         var bigInteger = Console.ReadLine().TrimStart(new char[] {'0'}).ToCharArray();//Ако е 0000005005 ще стане '5''0''0''5'!
  23.             var singleDigit = int.Parse(Console.ReadLine());
  24.             if (singleDigit == 0)
  25.             {
  26.                 Console.WriteLine(0);    
  27.                 return;
  28.             }
  29.             var result = new List<char>();      //Вместо char,може да е string.
  30.             var multiplyByOneDigit = "";        //Вместо "" ще е 0.
  31.             var onePlusMind = 0;
  32.             for (int i = bigInteger.Length - 1; i >= 0; i--)
  33.             {
  34.               multiplyByOneDigit = (singleDigit * int.Parse(bigInteger[i].ToString()) + onePlusMind).ToString();//Без .ToString()
  35.               if (singleDigit * int.Parse(bigInteger[i].ToString()) + onePlusMind > 9)
  36.               {
  37.                 onePlusMind = int.Parse(multiplyByOneDigit[0].ToString());//Тогава onePlusMind ще е onePlusMind /= 10;
  38.                 result.Insert(0, multiplyByOneDigit[1]);//И също вместо multiplyByOneDigit[1] ще е (multiplyByOneDigit %10).ToString()
  39.                 if (i == 0)
  40.                 {
  41.                    result.Insert(0, onePlusMind.ToString()[0]);//Без .ToString()[0]
  42.                 }
  43.               }
  44.               else
  45.               {
  46.                  onePlusMind = 0;
  47.                  result.Insert(0, multiplyByOneDigit[0]);//Без [0];
  48.               }
  49.            }
  50.             Console.WriteLine(string.Join("", result));
  51.     }
  52. }
Add Comment
Please, Sign In to add comment