You are viewing a single comment's thread. Return to all comments →
A long but easier understand approach , perhaps ...
static void Main(String[] args) { int n = Convert.ToInt32(Console.ReadLine()); int Quotient = n; int Remainder = 0; string BinaryValue = ""; while (Quotient > 0){ Remainder = Quotient % 2; BinaryValue = Remainder.ToString() + BinaryValue; Quotient = Quotient / 2; } Console.WriteLine (CheckConsecutive(BinaryValue)); } static string CheckConsecutive(string BinValue){ int HighestConsecutive = 0; int CurrentConsecutive = 0; for (int i = 0; i < BinValue.Length; i++){ if ( BinValue.Substring(i , 1) == "1" && i != BinValue.Length - 1){ CurrentConsecutive++; }else { if (i == BinValue.Length - 1 && BinValue.Substring(i , 1) == "1"){ CurrentConsecutive ++; } if (CurrentConsecutive > HighestConsecutive){ HighestConsecutive = CurrentConsecutive; } CurrentConsecutive = 0; } } return HighestConsecutive.ToString(); }
Day 10: Binary Numbers
You are viewing a single comment's thread. Return to all comments →
A long but easier understand approach , perhaps ...