-
Notifications
You must be signed in to change notification settings - Fork 22
/
EncryptedMatrix.cs
75 lines (63 loc) · 2.04 KB
/
EncryptedMatrix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
namespace _04.EncryptedMessage
{
using System;
using System.Text;
public class EncryptedMatrix
{
public static void Main()
{
string message = Console.ReadLine();
string diagonal = Console.ReadLine();
StringBuilder unencryptedNumber = new StringBuilder();
foreach (char symbol in message)
{
unencryptedNumber.Append(symbol % 10);
}
StringBuilder encryptedNumber = new StringBuilder();
for (int i = 0; i < unencryptedNumber.Length; i++)
{
int currentDigit = unencryptedNumber[i] - '0';
int resultingDigit;
if (currentDigit % 2 == 0)
{
resultingDigit = currentDigit * currentDigit;
}
else
{
int leftDigit = i == 0 ? 0 : unencryptedNumber[i - 1] - '0';
int rightDigit = i == unencryptedNumber.Length - 1 ? 0 : unencryptedNumber[i + 1] - '0';
resultingDigit = currentDigit + leftDigit + rightDigit;
}
encryptedNumber.Append(resultingDigit);
}
int position;
int update;
if (diagonal == "\\")
{
position = 0;
update = 1;
}
else
{
position = encryptedNumber.Length - 1;
update = -1;
}
for (int i = 0; i < encryptedNumber.Length; i++)
{
for (int j = 0; j < encryptedNumber.Length; j++)
{
if (j == position)
{
Console.Write(encryptedNumber[position] + " ");
}
else
{
Console.Write("0 ");
}
}
Console.WriteLine();
position += update;
}
}
}
}