Advertisement
Opteronic

Find Odd Digits And Sum Them

Dec 8th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. /*
  2.     Problem 2 – Nightmare on Code Street
  3.     On “Alexander Malinov” street there is one dark and spooky place
  4.     where everyone is getting goose bumps of. It is so scary that
  5.     even the vampires from Twilight do not want to get close to it.
  6.     The name is Nightmare Academy – dark and shadowy programmers
  7.     reside inside. The floor is green, the stairs are green, the
  8.     ceiling is green – brrrr, only the most fearsome and brave
  9.     warriors have survived!
  10.     Well, you do not have anything to worry, do you? You are in a
  11.     safe place now, are you? Nothing wrong can happen here, right?
  12.     Nice!
  13.     This problem is simple. You are given a text with some digits.
  14.     Your task is to find all digits in every odd position (starting
  15.     from zero) throughout the text and calculate their sum.
  16.     Input
  17.     The input data should be read from the console.
  18.     On the only input line you will receive the text.
  19.     The input data will always be valid and in the format described.
  20.     There is no need to check it explicitly.
  21.     Output
  22.     The output should be printed on the console.
  23.     On the only input line you should print the total amount of
  24.     digits in odd positions and their sum separated by space.
  25.     Constraints
  26.     The text’s length will be a valid integer number.
  27.     Allowed working time for your program: 0.10 seconds. Allowed
  28.     memory: 16 MB.
  29.     Examples
  30.     Input example
  31.     Output example
  32.     123
  33.     1 2
  34.  
  35.     10000
  36.     2 0
  37.  
  38.     987654
  39.     3 18
  40.  
  41.     5005005
  42.     3 5
  43.  
  44.     200000020
  45.     4 2
  46. */
  47.  
  48. using System;
  49. class FindOddDigitsAndSumThem
  50. {
  51.     static void Main()
  52.     {
  53.         int numI = int.Parse(Console.ReadLine());
  54.         int sum = 0;
  55.         int count = 0;
  56.         int len = GetStrLength(numI);
  57.         for (int i = 1; i <= len; i++)
  58.         {
  59.             int DigitNum = DefinePointedDigit(numI, (len - i + 1));
  60.             if (i % 2 == 0)
  61.             {
  62.                 sum = sum + DigitNum;
  63.                 count++;
  64.             }
  65.         }
  66.         Console.WriteLine(count + " " + sum);
  67.         Console.ReadKey();
  68.     }
  69.     //Method returns the digit pointed at position in the number
  70.     static int DefinePointedDigit(int number, int position)
  71.     {
  72.         int numXonN = (int)(Math.Exp((position - 1) * Math.Log(10)));
  73.         int numII = number / numXonN;
  74.         int digit = numII % 10;
  75.         return digit;
  76.     }
  77.     //Method returns the lenth of given Number
  78.     static int GetStrLength(int numToStr)
  79.     {
  80.         string strI = Convert.ToString(numToStr);
  81.         int length = strI.Length;
  82.         return length;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement