Created
March 13, 2016 08:39
-
-
Save Kiso-blg/79dd263e4796203dfcb6 to your computer and use it in GitHub Desktop.
C# Basics December 2014 Lab - 03.Byte_Party
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; | |
namespace _03.Byte_Party | |
{ | |
class ByteParty | |
{ | |
static void Main(string[] args) | |
{ | |
int length = int.Parse(Console.ReadLine()); | |
int[] table = GetSequence(length); | |
PerformCommands(table); | |
for (int i = 0; i < table.Length; i++) | |
{ | |
Console.WriteLine(table[i]); | |
} | |
} | |
static int[] GetSequence(int length) | |
{ | |
int[] seqence = new int[length]; | |
for (int i = 0; i < length; i++) | |
{ | |
seqence[i] = int.Parse(Console.ReadLine()); | |
} | |
return seqence; | |
} | |
static int[] PerformCommands(int[] sequence) | |
{ | |
string command = Console.ReadLine(); | |
while (command != "party over") | |
{ | |
string[] orderAndPosition = command.Split(' '); | |
int order = int.Parse(orderAndPosition[0]); | |
int position = int.Parse(orderAndPosition[1]); | |
if (order == -1) | |
{ | |
sequence = FlipBitAtPosition(sequence, position); | |
} | |
else if (order == 0) | |
{ | |
sequence = UnsetBitAtPosition(sequence, position); | |
} | |
else if (order == 1) | |
{ | |
sequence = SetBitAtPosition(sequence, position); | |
} | |
command = Console.ReadLine(); | |
} | |
return sequence; | |
} | |
private static int[] SetBitAtPosition(int[] sequence, int position) | |
{ | |
for (int i = 0; i < sequence.Length; i++) | |
{ | |
sequence[i] |= (1 << position); | |
} | |
return sequence; | |
} | |
private static int[] UnsetBitAtPosition(int[] sequence, int position) | |
{ | |
for (int i = 0; i < sequence.Length; i++) | |
{ | |
if (((sequence[i] >> position) & 1) == 1) | |
{ | |
sequence[i] ^= (1 << position); | |
} | |
} | |
return sequence; | |
} | |
static int[] FlipBitAtPosition(int[] sequence, int position) | |
{ | |
for (int i = 0; i < sequence.Length; i++) | |
{ | |
if (((sequence[i] >> position) & 1) == 1) | |
{ | |
sequence[i] ^= (1 << position); | |
} | |
else | |
{ | |
sequence[i] |= (1 << position); | |
} | |
} | |
return sequence; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment