fbinnzhivko

04.01 Encrypted Matrix

May 5th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. class EM
  5. {
  6.     public static void Main()
  7.     {
  8.         string input = Console.ReadLine();
  9.         byte[] array = Encoding.ASCII.GetBytes(input);
  10.  
  11.         List<int> numbers = new List<int>();
  12.         foreach (char ch in array)
  13.         {
  14.             numbers.Add(ch % 10);
  15.         }
  16.         List<int> cryptedNumbers = new List<int>();
  17.         for (int i = 0; i < numbers.Count; i++)
  18.         {
  19.             int currentDigit = numbers[i];
  20.             int result;
  21.  
  22.             if (currentDigit % 2 == 0)
  23.             {
  24.                 result = currentDigit * currentDigit;
  25.  
  26.                 if (result >= 10)
  27.                 {
  28.                     cryptedNumbers.Add(result / 10);
  29.                     cryptedNumbers.Add(result % 10);
  30.                 }
  31.                 else
  32.                 {
  33.                     cryptedNumbers.Add(result);
  34.                 }
  35.             }
  36.             else
  37.             {
  38.                 int previousDigit = i == 0 ? 0 : numbers[i - 1];
  39.                 int nextDigit = i == numbers.Count - 1 ? 0 : numbers[i + 1];
  40.                 result = currentDigit + previousDigit + nextDigit;
  41.                 if (result >= 10)
  42.                 {
  43.                     cryptedNumbers.Add(result / 10);
  44.                     cryptedNumbers.Add(result % 10);
  45.                 }
  46.                 else
  47.                 {
  48.                     cryptedNumbers.Add(result);
  49.                 }
  50.             }
  51.         }
  52.         string diagonal = Console.ReadLine();
  53.         int position;
  54.         int update;
  55.  
  56.         if (diagonal == "\\")
  57.         {
  58.             position = 0;
  59.             update = 1;
  60.         }
  61.         else
  62.         {
  63.             position = cryptedNumbers.Count - 1;
  64.             update = -1;
  65.         }
  66.  
  67.         for (int i = 0; i < cryptedNumbers.Count; i++)
  68.         {
  69.             for (int j = 0; j < cryptedNumbers.Count; j++)
  70.             {
  71.                 if (j == position)
  72.                 {
  73.                     Console.Write(cryptedNumbers[position] + " ");
  74.                 }
  75.                 else
  76.                 {
  77.                     Console.Write("0 ");
  78.                 }
  79.             }
  80.             Console.WriteLine();
  81.  
  82.             position += update;
  83.         }
  84.     }
  85. }
Add Comment
Please, Sign In to add comment