Created
December 25, 2023 01:48
-
-
Save unilecs/26abc5bc60104341e2e4e48defeb898f 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; | |
public class Program | |
{ | |
public static string ConvertNumToBinaryString(int num, int length) | |
{ | |
string binary = Convert.ToString(num, 2); | |
// добавляем ведущие нули до размера length | |
// Например, length = 5, binary = 101 -> "00101" | |
if (binary.Length < length) | |
{ | |
binary = string.Concat(System.Linq.Enumerable.Repeat("0", length - binary.Length)) + binary; | |
} | |
return binary; | |
} | |
public static string FindMissedBinaryString(string[] nums) { | |
var set = new HashSet<string>(nums); | |
var len = nums[0].Length; | |
int digitNum = (int)Math.Pow(2, len); | |
for (int i = 0; i < digitNum; i++) | |
{ | |
string binary = ConvertNumToBinaryString(i, len); | |
if (!set.Contains(binary)) | |
{ | |
return binary; | |
} | |
} | |
return ""; | |
} | |
public static void Main() | |
{ | |
Console.WriteLine("UniLecs"); | |
// tests | |
Console.WriteLine(FindMissedBinaryString(new string[] { "01", "10" })); // "00" | |
Console.WriteLine(FindMissedBinaryString(new string[] { "000", "111","011","001" })); // "010" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment