Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://www.hackerrank.com/challenges/30-binary-numbers
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- class Solution
- {
- static void Main(String[] args)
- {
- int n = Convert.ToInt32(Console.ReadLine());
- //string result = string.Concat("új szám", "régi szám");
- string binaryNumber = toBinary(n);
- Console.WriteLine(ones(binaryNumber));
- }
- static string toBinary(int n)
- {
- string result = "";
- while (n != 0)
- {
- result = string.Concat((n%2), result);
- n = n / 2;
- }
- return result;
- }
- static int ones(string binary)
- {
- int maxOnes = 0;
- int db = 0;
- for (int i = 0; i < binary.Length; i++)
- {
- if(binary[i] == '1')
- {
- db++;
- }
- else
- {
- if ((binary[i-1] == '1') && (maxOnes < db))
- {
- maxOnes = db;
- }
- db = 0;
- }
- }
- if(maxOnes < db)
- {
- maxOnes = db;
- }
- return maxOnes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement