Advertisement
dimipan80

Letters change Numbers

May 12th, 2015
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. /* You get a string consisting of a number between two letters. Depending on whether the letter was in front of the number or after it you would perform different mathematical operations on the number to achieve the result.
  2.  * First you start with the letter before the number. If it's Uppercase you divide the number by the letter's position in the alphabet. If it's lowercase you multiply the number with the letter's position. Then you move to the letter after the number. If it's Uppercase you subtract its position from the resulted number. If it's lowercase you add its position to the resulted number. But the game became too easy for Nakov really quick. He decided to complicate it a bit by doing the same but with multiple strings keeping track of only the total sum of all results. Once he started to solve this with more strings and bigger numbers it became quite hard to do it only in his mind. So he kindly asks you to write a program that calculates the sum of all numbers after the operations on each number have been done.
  3.  * The input comes from the console as a single line, holding the sequence of strings. Strings are separated by one or more white spaces.
  4.  * Print at the console a single number: the total sum of all processed numbers rounded up to two digits after the decimal separator.
  5.  * Constraints: The count of the strings will be in the range [1 … 10].
  6.  * The numbers between the letters will be integers in range [1 … 2 147 483 647]. */
  7.  
  8. namespace _07.LettersChangeNumbers
  9. {
  10.     using System;
  11.  
  12.     class LettersChangeNumbers
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             string[] strings = Console.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  17.  
  18.             decimal sum = CalculateTotalSumOfGivenStrings(strings);
  19.             Console.WriteLine("{0:F2}", sum);
  20.         }
  21.  
  22.         private static decimal CalculateTotalSumOfGivenStrings(string[] strings)
  23.         {
  24.             decimal sum = 0;
  25.             foreach (string str in strings)
  26.             {
  27.                 char firstLetter = str[0];
  28.                 char lastLetter = str[str.Length - 1];
  29.                 long number = long.Parse(str.Substring(1, str.Length - 2));
  30.                 sum += (char.IsUpper(firstLetter)) ?
  31.                     (decimal)number / ((int)firstLetter - 64) :
  32.                     (decimal)number * ((int)firstLetter - 96);
  33.  
  34.                 sum += (char.IsUpper(lastLetter)) ?
  35.                     -(decimal)((int)lastLetter - 64) :
  36.                     (decimal)((int)lastLetter - 96);
  37.             }
  38.  
  39.             return sum;
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement