Advertisement
svetlozar_kirkov

Bits Inverter

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