Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Game of Bits
- //Vasko likes to play with odd and even numbers as well as bits. He has to make a game using bits but he really enjoys the course //of Web Fundamentals so he doesn't have time to make the game. Please help him!
- //You have a 32-bit integer and commands: "Odd", "Even" or "Game Over!" When you read the "Odd" command you have to obtain a new //number by extracting the values of all odd bit positions in the current number (positions are counted from right to left and the //first bit has a position of 1). When you read the "Even" command you have to extract the bits at even positions. When you read //the command "Game Over!" you must print on the console the count of bits with value '1' in the final number.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication5
- {
- class Program
- {
- static void Main(string[] args)
- {
- long num = long.Parse(Console.ReadLine());
- string bin = Convert.ToString(num, 2);
- string command = Console.ReadLine();
- long bit = 0;
- long value = 0;
- int index1 = 0;
- int indexForBit = 0;
- long rememberedNum = num;
- while (command != "Game Over!")
- {
- if (command == "Odd")
- {
- for (int i = bin.Length ; i >= 0 ; i--)
- {
- if (index1 % 2 == 0)
- {
- bit = num & (1 << index1);
- bit = bit >> index1;
- value = value | (bit << indexForBit);
- indexForBit++;
- }
- else
- {
- }
- index1++;
- }
- rememberedNum = value;
- num = value;
- bin = Convert.ToString(num, 2);
- index1 = 0;
- indexForBit = 0;
- value = 0;
- }
- else if (command == "Even")
- {
- for (int j = bin.Length; j >= 0; j--)
- {
- if (index1 % 2 != 0)
- {
- bit = num & (1 << index1);
- bit = bit >> index1;
- value = value | (bit << indexForBit);
- indexForBit++;
- }
- else
- {
- }
- index1++;
- }
- rememberedNum = value;
- num = value;
- bin = Convert.ToString(num, 2);
- index1 = 0;
- indexForBit = 0;
- value = 0;
- }
- command = Console.ReadLine();
- }
- string binary = Convert.ToString(rememberedNum, 2);
- int numberOfOnes = 0;
- for (int p = 0; p < binary.Length; p++)
- {
- if (binary[p] == '1')
- {
- numberOfOnes++;
- }
- }
- Console.WriteLine("{0} -> {1}",rememberedNum,numberOfOnes);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment