Advertisement
mustafov

Students to Students and Bits to Bits

Sep 3rd, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. // Students to Students and Bits to Bits
  2. //You are given a list of N numbers.
  3. //Get the most right 30 bits of every number and concatenate them.
  4. //Write a program to find the length of the longest sequence of zeroes and the length of the longest sequence of ones from the //obtained concatenated sequence.
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. namespace ConsoleApplication5
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             int n = int.Parse(Console.ReadLine());
  19.             int ones = 1;
  20.             int zeroes = 1;
  21.             int maxOnes = 0;
  22.             int maxZeroes = 0;
  23.             StringBuilder bin = new StringBuilder ();
  24.             int ind = 0;
  25.             for (int i = 0; i < n ; i++)
  26.             {
  27.                
  28.                 int number = int.Parse(Console.ReadLine());
  29.                 string binary = Convert.ToString(number, 2).PadLeft(30, '0');
  30.                 bin.Insert(ind, binary);
  31.                 ind += 30;
  32.             }
  33.                 for (int j = 0; j < bin.Length; j++)
  34.                 {
  35.                     if (bin[j] == '1' && j != bin.Length - 1)
  36.                     {
  37.                         if (bin[j] == '1' && bin[j + 1] == '1')
  38.                         {
  39.                             ones++;
  40.                         }
  41.                     }
  42.                     if (bin[j] == '1' && j != bin.Length - 1)
  43.                     {
  44.                         if (bin[j] == '1' && bin[j + 1] != '1')
  45.                         {
  46.                             if (ones > maxOnes)
  47.                             {
  48.                                 maxOnes = ones;
  49.                             }
  50.                             ones = 1;
  51.                         }
  52.                        
  53.                     }
  54.                     if (bin[j] == '1' && j == bin.Length - 1)
  55.                     {
  56.                         if (ones > maxOnes)
  57.                         {
  58.                             maxOnes = ones;
  59.                         }
  60.                         ones = 1;
  61.                     }
  62.  
  63.                     if (bin[j] == '0' && j != bin.Length - 1)
  64.                     {
  65.                         if (bin[j] == '0' && bin[j + 1] == '0')
  66.                         {
  67.                             zeroes++;
  68.                         }
  69.                     }
  70.                     if (bin[j] == '0' && j != bin.Length - 1)
  71.                     {
  72.                         if (bin[j] == '0' && bin[j + 1] != '0')
  73.                         {
  74.                             if (zeroes > maxZeroes)
  75.                             {
  76.                                 maxZeroes = zeroes;
  77.                             }
  78.                             zeroes = 1;
  79.                         }
  80.                        
  81.                     }
  82.                     if (bin[j] == '0' && j == bin.Length - 1)
  83.                     {
  84.                         if (zeroes > maxZeroes)
  85.                         {
  86.                             maxZeroes = zeroes;
  87.                         }
  88.                         zeroes = 1;
  89.                     }
  90.                 }
  91.            
  92.             Console.WriteLine(maxZeroes);
  93.             Console.WriteLine(maxOnes);
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement