Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 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.  
  7. namespace ConsoleApplication5
  8. {
  9.     class Program
  10.     {
  11.         static int invert(int N, int step)
  12.         {
  13.             int chislo;
  14.             double hundred, hundred2;
  15.             N =  int.Parse(Convert.ToString(N, 2));
  16.             for (int i = 0; i < step; i++)
  17.             {
  18.                 hundred = Math.Pow(10, i);
  19.                 hundred2 = Math.Pow(10,step - i);
  20.                 Console.WriteLine(hundred);
  21.                 Console.WriteLine(hundred2);
  22.                 chislo = Convert.ToInt32(N % hundred / (hundred / 10));
  23.                 N = Convert.ToInt32(N - chislo * (hundred / 10));
  24.                 N = Convert.ToInt32(N + chislo * (hundred2 / 10));
  25.             }
  26.             return N;
  27.         }
  28.         static int changer(int N, int p, int m)
  29.         { //N - Исходное число, p - позиция с которой инвертируем биты, m - кол-во инвертируемых битов
  30.             int step = Convert.ToInt32(Math.Round(Math.Sqrt(N-1)+1));
  31.             Console.WriteLine(step);
  32.             N = invert(N, step);
  33.             double hundreds;
  34.             int chislo;
  35.             for (int i = p + m - 1; i >= p; i--)
  36.             {
  37.                 hundreds = Convert.ToInt32(Math.Pow(10, i));
  38.                 chislo = Convert.ToInt32((N % hundreds) / (hundreds / 10));
  39.                 if (chislo == 1)
  40.                 {
  41.                     N = Convert.ToInt32(N - (hundreds / 10));
  42.                 }
  43.                 else if (chislo == 0)
  44.                 {
  45.                     N = Convert.ToInt32(N + (hundreds / 10));
  46.                 }
  47.             }
  48.             N = invert(N, step);
  49.             string bin = Convert.ToString(N);
  50.             N = Convert.ToInt32(bin, 2);
  51.  
  52.             return N;
  53.         }
  54.         static void Main(string[] args)
  55.         {
  56.             bool f1, f2, f3;
  57.             int N = 0; int p = 0; int m = 0;
  58.             Console.WriteLine("Введите число в котором необходимо инвертировать биты: ");
  59.             f1 = int.TryParse(Console.ReadLine(), out N);
  60.             Console.WriteLine("Введите символ с которого начинается инвертация битов: ");
  61.             f2 = int.TryParse(Console.ReadLine(), out p);
  62.             Console.WriteLine("Введите кол-во инвертируемых битов: ");
  63.             f3 = int.TryParse(Console.ReadLine(), out m);
  64.             if (f1 && f2 && f3)
  65.             {
  66.                 double f4 = Convert.ToInt32(Math.Round(Math.Sqrt(N - 1) + 1)); //Узнаем сколько символов в двоичном представлении числа
  67.                 Console.WriteLine(f4);
  68.                 if (f4 - m - p + 1 >= 0 && p != 0)
  69.                 {
  70.                     Console.WriteLine(changer(N, p, m));
  71.                 }
  72.                 else {
  73.                     Console.WriteLine("ERROR!");
  74.                 }
  75.             }
  76.             else {
  77.                 Console.WriteLine("ERROR!");
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement