Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Problem 2 – Nightmare on Code Street
- 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!
- 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!
- 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.
- Input
- The input data should be read from the console.
- On the only input line you will receive the text.
- The input data will always be valid and in the format described.
- There is no need to check it explicitly.
- Output
- The output should be printed on the console.
- On the only input line you should print the total amount of
- digits in odd positions and their sum separated by space.
- Constraints
- The text’s length will be a valid integer number.
- Allowed working time for your program: 0.10 seconds. Allowed
- memory: 16 MB.
- Examples
- Input example
- Output example
- 123
- 1 2
- 10000
- 2 0
- 987654
- 3 18
- 5005005
- 3 5
- 200000020
- 4 2
- */
- using System;
- class FindOddDigitsAndSumThem
- {
- static void Main()
- {
- int numI = int.Parse(Console.ReadLine());
- int sum = 0;
- int count = 0;
- int len = GetStrLength(numI);
- for (int i = 1; i <= len; i++)
- {
- int DigitNum = DefinePointedDigit(numI, (len - i + 1));
- if (i % 2 == 0)
- {
- sum = sum + DigitNum;
- count++;
- }
- }
- Console.WriteLine(count + " " + sum);
- Console.ReadKey();
- }
- //Method returns the digit pointed at position in the number
- static int DefinePointedDigit(int number, int position)
- {
- int numXonN = (int)(Math.Exp((position - 1) * Math.Log(10)));
- int numII = number / numXonN;
- int digit = numII % 10;
- return digit;
- }
- //Method returns the lenth of given Number
- static int GetStrLength(int numToStr)
- {
- string strI = Convert.ToString(numToStr);
- int length = strI.Length;
- return length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement