svetlozar_kirkov

Spy Hard (C# Exam)

Apr 6th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using System;
  2.  
  3. namespace SpyHard
  4. {
  5.     class SpyHard
  6.     {
  7.         static void Main()
  8.         {
  9.             int key = int.Parse(Console.ReadLine());
  10.             string inputMsg = Console.ReadLine();
  11.             string inputMsgLower = inputMsg.ToLower();
  12.             int symbolsSum = 0;
  13.             for (int i = 0; i < inputMsg.Length; i++)
  14.             {
  15.                 if (Char.IsLetter(inputMsgLower[i]))
  16.                 {
  17.                     symbolsSum += (int)inputMsgLower[i] - 96;
  18.                 }
  19.                 else
  20.                 {
  21.                     symbolsSum += (int)inputMsgLower[i];
  22.                 }
  23.             }
  24.             Console.WriteLine(key.ToString() + inputMsg.Length.ToString() +
  25.                                 DecimalToArbitrarySystem(symbolsSum, key));
  26.  
  27.         }
  28.         public static string DecimalToArbitrarySystem(long decimalNumber, int radix)
  29.         {
  30.             const int BitsInLong = 64;
  31.             const string Digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  32.  
  33.             if (radix < 2 || radix > Digits.Length)
  34.                 throw new ArgumentException("The radix must be >= 2 and <= " + Digits.Length.ToString());
  35.  
  36.             if (decimalNumber == 0)
  37.                 return "0";
  38.  
  39.             int index = BitsInLong - 1;
  40.             long currentNumber = Math.Abs(decimalNumber);
  41.             char[] charArray = new char[BitsInLong];
  42.  
  43.             while (currentNumber != 0)
  44.             {
  45.                 int remainder = (int)(currentNumber % radix);
  46.                 charArray[index--] = Digits[remainder];
  47.                 currentNumber = currentNumber / radix;
  48.             }
  49.  
  50.             string result = new String(charArray, index + 1, BitsInLong - index - 1);
  51.             if (decimalNumber < 0)
  52.             {
  53.                 result = "-" + result;
  54.             }
  55.  
  56.             return result;
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment