Created
August 17, 2014 10:31
-
-
Save boyanov83/48f89dac156d6e959ede to your computer and use it in GitHub Desktop.
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.Collections.Generic; | |
using System.Linq; | |
using System.Numerics; | |
using System.Text; | |
using System.Threading.Tasks; | |
class ChangeEvenBits | |
{ | |
static void Main() | |
{ | |
int numbers = int.Parse(Console.ReadLine()); | |
BigInteger[] bits = new BigInteger[numbers]; | |
for (int i = 0; i < numbers; i++) | |
{ | |
bits[i] = BigInteger.Parse(Console.ReadLine()); | |
} | |
BigInteger L = BigInteger.Parse(Console.ReadLine()); | |
string stringL = ConvertToBinary(L).PadLeft(64, '0'); | |
List<char> list = stringL.ToList(); | |
int replaced = 0; | |
for (int k = 0; k < numbers; k++) | |
{ | |
string current = ConvertToBinary(bits[k]); | |
list.Reverse(); | |
int positions = 0; | |
for (int p = 0; p < current.Length; p ++) | |
{ | |
if (list[positions] == '0') | |
{ | |
list[positions] = '1'; | |
replaced++; | |
} | |
positions += 2; | |
} | |
list.Reverse(); | |
} | |
string result = ""; | |
foreach (var item in list) | |
{ | |
result += item; | |
} | |
Print(result); | |
Console.WriteLine(replaced); | |
} | |
private static void Print(string input) | |
{ | |
BigInteger currentNum = BigInteger.Parse(input); | |
long sum = 0; | |
long sabiratel = 1; | |
for (long i = 0; i < input.Length; i++) | |
{ | |
if (currentNum % 10 == 1) | |
{ | |
sum += sabiratel; | |
} | |
currentNum /= 10; | |
sabiratel *= 2; | |
} | |
Console.WriteLine(sum); | |
} | |
public static string ConvertToBinary(BigInteger value) | |
{ | |
if (value == 0) return "0"; | |
System.Text.StringBuilder b = new System.Text.StringBuilder(); | |
while (value != 0) | |
{ | |
b.Insert(0, ((value & 1) == 1) ? '1' : '0'); | |
value >>= 1; | |
} | |
return b.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment