Advertisement
dimipan80

Calculation Problem

May 26th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. /* Your task is to help the global programmer community. You will receive a set on letter-numbers (strings) on one line separated with a single space ‘ ‘. Sum all of the letter-numbers and print out the resulting sum both in the 23-based system and decimal system. The digits go as following: a -> 0, b -> 1, c -> 2, d -> 3 ... j -> 9, k -> 10 ... v -> 21, w -> 22.
  2.  * Print out the sum of the given numbers in the format “[sum in 23-base numeral system] = [sum in decimal system]”. */
  3.  
  4. namespace Calculation_Problem
  5. {
  6.     using System;
  7.     using System.Linq;
  8.     using System.Text;
  9.  
  10.     class CalculationProblem
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string[] words = Console.ReadLine().Split();
  15.             int totalSum = words
  16.                 .Sum(word => word
  17.                     .Select((t, i) => (int)((t - 97) * Math.Pow(23, word.Length - i - 1)))
  18.                     .Sum());
  19.  
  20.             StringBuilder sb = new StringBuilder();
  21.             int quotient = 0;
  22.             int temp = totalSum;
  23.             do
  24.             {
  25.                 quotient = temp / 23;
  26.                 int remaider = temp % 23;
  27.                 sb.Append((char)(remaider + 97));
  28.                 temp = quotient;
  29.             } while (quotient != 0);
  30.  
  31.             string sumIn23NumSystem = sb.ToString();
  32.             char[] letters = sumIn23NumSystem.ToCharArray();
  33.             Array.Reverse(letters);
  34.  
  35.             Console.WriteLine("{0} = {1}", new String(letters), totalSum);
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement