svetlozar_kirkov

Bits Killer (100/100)

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