Advertisement
DennyGD

NightmareOnCodeStreet

Jan 17th, 2015
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. Problem 2 – Nightmare on Code Street
  2. On “Alexander Malinov” street there is one dark and spooky place where everyone is getting goose bumps of. It is so scary that even the vampires from Twilight do not want to get close to it. The name is Nightmare Academy – dark and shadowy programmers reside inside. The floor is green, the stairs are green, the ceiling is green – brrrr, only the most fearsome and brave warriors have survived!
  3. Well, you do not have anything to worry, do you? You are in a safe place now, are you? Nothing wrong can happen here, right? Nice!
  4. This problem is simple. You are given a text with some digits. Your task is to find all digits in every odd position (starting from zero) throughout the text and calculate their sum.
  5. Input
  6. The input data should be read from the console.
  7. On the only input line you will receive the text.
  8. The input data will always be valid and in the format described. There is no need to check it explicitly.
  9. Output
  10. The output should be printed on the console.
  11. On the only input line you should print the total amount of digits in odd positions and their sum separated by space.
  12. Constraints
  13. • The text’s length will be a valid integer number.
  14. • Allowed working time for your program: 0.10 seconds. Allowed memory: 16 MB.
  15. Examples
  16. Input example   Output example
  17. 123 1 2
  18. 10000   2 0
  19. 987654  3 18
  20. 5005005 3 5
  21. 200000020   4 2
  22.  
  23. using System;
  24.  
  25. namespace NightmareOnCodeStreet
  26. {
  27.     class NightmareOnCodeStreet
  28.     {
  29.         static void Main()
  30.         {
  31.             string input = Console.ReadLine();
  32.            
  33.  
  34.             int sum = 0;
  35.             int count = 0;
  36.             int oddTracker = 0;
  37.  
  38.             foreach (char symbol in input)
  39.             {
  40.                 if ((oddTracker % 2 != 0))
  41.                 {
  42.                     if (symbol >= 48 && symbol <= 57)
  43.                     {
  44.                         int current = symbol - 48;
  45.                         sum += current;
  46.                         count++;
  47.                     }
  48.                 }
  49.                 oddTracker++;
  50.             }
  51.  
  52.             Console.WriteLine("{0} {1}", count, sum);
  53.            
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement