Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MagicQuad
  6. {    class Program
  7.     {        static void Main(string[] args)
  8.         { string _randomLine = "ЙЦУКЕНГШЩЗХФЪЫВАПРОЛДЖЭЧСМИТЬБЮ";
  9.             Random _rand = new Random();
  10.             Console.WriteLine("Введите сообщение:");
  11.             String _line = Console.ReadLine().ToUpper().Replace(" ", "");
  12.             int _d = (int)Math.Ceiling(Math.Sqrt(_line.Length));
  13.             if (_d % 2 != 1)
  14.                 _d++;
  15.             Console.WriteLine("Магический квадрат: " + _d.ToString() + "\n");
  16.             int[,] _quad = new int[_d, _d];
  17.             for (int j = 0; j < _d; j++)
  18.             { for (int i = 0; i < _d; i++)
  19. { _quad[i, j] = _d*(((i + 1) + (j + 1) - 1 + (_d / 2)) % _d)+(((i+1) + 2*(j+1) - 2) % _d) + 1;
  20.                     Console.Write(_quad[i, j].ToString() + "\t");                }
  21.                 Console.WriteLine();            }
  22.             Console.WriteLine("Шифрование сообщения:");
  23.             string _cryptedString = "";
  24.             for (int j = 0; j < _d; j++)
  25.             { for (int i = 0; i < _d; i++)
  26.                 { if ((_quad[i, j] - 1) < _line.Length)
  27.                     { Console.Write(_line[_quad[i, j] - 1] + "\t");
  28.                         _cryptedString += _line[_quad[i, j] - 1];                    }
  29.                else
  30.                   { char _randomChar = _randomLine[_rand.Next(0, _randomLine.Length - 1)];
  31.                         Console.Write(_randomChar + "\t");
  32.                         _cryptedString += _randomChar;                    }                }
  33.                 Console.WriteLine();            }
  34.             Console.WriteLine("\nЗашифрованная строка:");
  35.             Console.WriteLine(_cryptedString);
  36.             Console.ReadKey();        }    }    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement