İçeriğe geç

Convert to Base 2 and Find Consecutive True Bits

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

static class Solution {

	public static Stack ToBinary(this int number) {

		bool reminder;
		Stack bits = new Stack();
		while (number > 0) {
			reminder = (number % 2) == 1 ? true: false;
			number = number / 2;
			bits.Push(reminder);
		}

		return bits;
	}

	static void Main(string[] args) {
		int n = Convert.ToInt32(Console.ReadLine());
		var bits = n.ToBinary();

		byte maxConsecutive = 0;
		byte consecutive = 0;
		foreach(var bit in bits) {
			if (bit) {
				consecutive++;
				if (maxConsecutive <= consecutive) {
					maxConsecutive = consecutive;
				}
			} else {
				consecutive = 0;
			}
		}
		System.Console.WriteLine(maxConsecutive);
	}
}
Kategori:C#

İlk Yorumu Siz Yapın

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir