Created
March 13, 2016 09:32
-
-
Save Kiso-blg/3b581ae53e63c7ad7b42 to your computer and use it in GitHub Desktop.
Basics Exam 20 December 2014
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
namespace Problem_5___Bit_Lock | |
{ | |
class BitLock | |
{ | |
static void Main(string[] args) | |
{ | |
int[] sequence = Console | |
.ReadLine() | |
.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) | |
.Select(item => int.Parse(item)) | |
.ToArray(); | |
while (true) | |
{ | |
string input = Console.ReadLine().ToLower(); | |
if (input == "end") | |
{ | |
break; | |
} | |
string[] commands = input.Split(' '); | |
int row; | |
if (commands[0] == "check") | |
{ | |
string command = commands[0]; | |
int column = int.Parse(commands[1]); | |
CheckColumn(sequence, column); | |
} | |
else if (int.TryParse(commands[0], out row)) | |
{ | |
string direction = commands[1]; | |
int rollsCount = int.Parse(commands[2]); | |
RollNumber(sequence, row, direction, rollsCount); | |
} | |
} | |
PrintArray(sequence); | |
Console.WriteLine(); | |
} | |
private static void PrintArray(int[] array) | |
{ | |
for (int i = 0; i < array.Length; i++) | |
{ | |
Console.Write("{0} ", array[i]); | |
} | |
} | |
private static void RollNumber(int[] sequence, int row, string direction, int rollsCount) | |
{ | |
if (direction == "right") | |
{ | |
for (int i = 0; i < rollsCount; i++) | |
{ | |
int bit = sequence[row] & 1; | |
sequence[row] >>= 1; | |
sequence[row] |= (bit << 11); | |
} | |
} | |
else if (direction == "left") | |
{ | |
for (int i = 0; i < rollsCount; i++) | |
{ | |
int bit = (sequence[row] >> 11) & 1; | |
sequence[row] |= (1 << 11); | |
sequence[row] ^= (1 << 11); | |
sequence[row] <<= 1; | |
sequence[row] |= bit; | |
} | |
} | |
} | |
private static void CheckColumn(int[] array, int col) | |
{ | |
int bitsCount = 0; | |
for (int row = 0; row < array.Length; row++) | |
{ | |
if (((array[row] >> col) & 1) == 1) | |
{ | |
bitsCount++; | |
} | |
} | |
Console.WriteLine(bitsCount); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment