Advertisement
Guest User

Untitled

a guest
Feb 4th, 2015
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Numerics;
  7.  
  8. namespace _05.Problem
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int n = int.Parse(Console.ReadLine());
  15.             StringBuilder sb = new StringBuilder();
  16.  
  17.  
  18.             long[] numbers = new long[n];
  19.             string[] binary = new string[n];
  20.             int count = 30;
  21.             for (int i = 0; i < n; i++)
  22.             {
  23.                 numbers[i] = long.Parse(Console.ReadLine());
  24.  
  25.             }
  26.  
  27.             for (int i = 0; i < numbers.Length; i++)
  28.             {
  29.                  binary[i] = Convert.ToString(numbers[i], 2);
  30.                
  31.                 int difference = -1;
  32.                 if (binary[i].Length >= 30)
  33.                 {
  34.                     count = 30;
  35.          
  36.                 }
  37.                 else
  38.                 {                  
  39.                     count = binary[i].Length;
  40.                     difference = 30 - count;
  41.                     for (int k = 0; k < difference; k++)
  42.                     {
  43.                         sb.Append("0");
  44.                     }
  45.  
  46.                 }
  47.  
  48.                 difference = 30 - count;
  49.                  for (int k = 0; k < count; k++)
  50.                  {
  51.                          sb.Append(binary[i][k] - '0');
  52.                  }
  53.             }
  54.  
  55.             string bits = sb.ToString();
  56.             int zerosCount = 0;
  57.             int onesCount = 0;
  58.             int longestZeroCount = 0;
  59.             int longestOnesCount = 0;
  60.  
  61.             for (int i = 0; i < bits.Length; i++)
  62.             {
  63.                 if ((bits[i] - '0') == 0)
  64.                 {
  65.                     zerosCount++;
  66.                     if (longestOnesCount < onesCount)
  67.                     {
  68.                         longestOnesCount = onesCount;
  69.                     }
  70.                     onesCount = 0;
  71.                 }
  72.                 else if ((bits[i] - '0') == 1)
  73.                 {
  74.                     onesCount++;
  75.                     if (longestZeroCount < zerosCount)
  76.                     {
  77.                         longestZeroCount = zerosCount;
  78.                     }
  79.                     zerosCount = 0;
  80.                 }
  81.                 if (onesCount > longestOnesCount)
  82.                 {
  83.                     longestOnesCount = onesCount;
  84.                 }
  85.                 if (zerosCount > longestZeroCount)
  86.                 {
  87.                     longestZeroCount = zerosCount;
  88.                 }
  89.             }
  90.          
  91.             Console.WriteLine(longestZeroCount);
  92.             Console.WriteLine(longestOnesCount);
  93.  
  94.            
  95.         }
  96.     }
  97. }
  98.  
  99. // 100
  100. // 1073741823
  101. // 1073741823
  102. // 1073741823
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement