svetlozar_kirkov

Catch The Bits

Sep 23rd, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace CatchTheBits
  7. {
  8.     class CatchTheBits
  9.     {
  10.         static void Main()
  11.         {
  12.             int n = int.Parse(Console.ReadLine());
  13.             int step = int.Parse(Console.ReadLine());
  14.             string[] binaries = new string[n];
  15.             List<char> collected = new List<char>();
  16.  
  17.             for (int i = 0; i < n; i++)
  18.             {
  19.                 binaries[i] = Convert.ToString(int.Parse(Console.ReadLine()), 2).PadLeft(8, '0');
  20.             }
  21.  
  22.             string joinedBinary = string.Join("", binaries);
  23.  
  24.             for (int i = 0, j = 1; i < joinedBinary.Length; i++)
  25.             {
  26.                 if (i == 1)
  27.                 {
  28.                     collected.Add(joinedBinary[i]);
  29.                 }
  30.                 else if (i == 1+j*step)
  31.                 {
  32.                     collected.Add(joinedBinary[i]);
  33.                     j++;
  34.                 }
  35.             }
  36.  
  37.             string joined = string.Join("", collected);
  38.  
  39.             if (joined.Length % 8 == 0)
  40.             {
  41.                 var final = Split(joined, 8);
  42.                 foreach (var item in final)
  43.                 {
  44.                     Console.WriteLine(Convert.ToInt32(item, 2));
  45.                 }
  46.             }
  47.             else
  48.             {
  49.                 string finalJoin = joined.PadRight(joined.Length+(8-(joined.Length%8)),'0');
  50.                 var final = Split(finalJoin, 8);
  51.                 foreach (var item in final)
  52.                 {
  53.                     Console.WriteLine(Convert.ToInt32(item, 2));
  54.                 }
  55.             }
  56.         }
  57.         static IEnumerable<string> Split(string str, int chunkSize)
  58.         {
  59.             int len = str.Length;
  60.             return Enumerable.Range(0, len / chunkSize).Select(i => str.Substring(i * chunkSize, chunkSize));
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment