Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication5
- {
- class Program
- {
- static int invert(int N, int step)
- {
- int chislo;
- double hundred, hundred2;
- N = int.Parse(Convert.ToString(N, 2));
- for (int i = 0; i < step; i++)
- {
- hundred = Math.Pow(10, i);
- hundred2 = Math.Pow(10,step - i);
- Console.WriteLine(hundred);
- Console.WriteLine(hundred2);
- chislo = Convert.ToInt32(N % hundred / (hundred / 10));
- N = Convert.ToInt32(N - chislo * (hundred / 10));
- N = Convert.ToInt32(N + chislo * (hundred2 / 10));
- }
- return N;
- }
- static int changer(int N, int p, int m)
- { //N - Исходное число, p - позиция с которой инвертируем биты, m - кол-во инвертируемых битов
- int step = Convert.ToInt32(Math.Round(Math.Sqrt(N-1)+1));
- Console.WriteLine(step);
- N = invert(N, step);
- double hundreds;
- int chislo;
- for (int i = p + m - 1; i >= p; i--)
- {
- hundreds = Convert.ToInt32(Math.Pow(10, i));
- chislo = Convert.ToInt32((N % hundreds) / (hundreds / 10));
- if (chislo == 1)
- {
- N = Convert.ToInt32(N - (hundreds / 10));
- }
- else if (chislo == 0)
- {
- N = Convert.ToInt32(N + (hundreds / 10));
- }
- }
- N = invert(N, step);
- string bin = Convert.ToString(N);
- N = Convert.ToInt32(bin, 2);
- return N;
- }
- static void Main(string[] args)
- {
- bool f1, f2, f3;
- int N = 0; int p = 0; int m = 0;
- Console.WriteLine("Введите число в котором необходимо инвертировать биты: ");
- f1 = int.TryParse(Console.ReadLine(), out N);
- Console.WriteLine("Введите символ с которого начинается инвертация битов: ");
- f2 = int.TryParse(Console.ReadLine(), out p);
- Console.WriteLine("Введите кол-во инвертируемых битов: ");
- f3 = int.TryParse(Console.ReadLine(), out m);
- if (f1 && f2 && f3)
- {
- double f4 = Convert.ToInt32(Math.Round(Math.Sqrt(N - 1) + 1)); //Узнаем сколько символов в двоичном представлении числа
- Console.WriteLine(f4);
- if (f4 - m - p + 1 >= 0 && p != 0)
- {
- Console.WriteLine(changer(N, p, m));
- }
- else {
- Console.WriteLine("ERROR!");
- }
- }
- else {
- Console.WriteLine("ERROR!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement