Created
April 22, 2016 05:55
-
-
Save Kiso-blg/a6626bc30f34b7e147a1dd60a35c7caf to your computer and use it in GitHub Desktop.
Problem_4___Encrypted_matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
namespace Problem_4___Encrypted_matrix | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Stream inStream = Console.OpenStandardInput(1000); | |
Console.SetIn(new StreamReader(inStream, Console.InputEncoding, false, 1000)); | |
string message = Console.ReadLine(); | |
char diagonal = char.Parse(Console.ReadLine()); | |
char backSlash = '\\'; | |
string convertMsg = ConvertMessage(message); | |
string encryptedMessage = Encrypt(convertMsg); | |
if (diagonal == backSlash) | |
{ | |
PrintMainDiagonal(encryptedMessage); | |
} | |
else | |
{ | |
PrintAntiDiagonal(encryptedMessage); | |
} | |
} | |
private static void PrintAntiDiagonal(string encryptedMessage) | |
{ | |
for (int col = encryptedMessage.Length - 1; col > -1; col--) | |
{ | |
for (int row = 0; row < encryptedMessage.Length; row++) | |
{ | |
Console.Write(row == col ? encryptedMessage[row] : '0'); | |
Console.Write(' '); | |
} | |
Console.WriteLine(); | |
} | |
} | |
private static void PrintMainDiagonal(string encryptedMessage) | |
{ | |
for (int row = 0; row < encryptedMessage.Length; row++) | |
{ | |
for (int col = 0; col < encryptedMessage.Length; col++) | |
{ | |
Console.Write(row == col ? encryptedMessage[col] : '0'); | |
Console.Write(' '); | |
} | |
Console.WriteLine(); | |
} | |
} | |
private static string Encrypt(string word) | |
{ | |
string text = ""; | |
for (int i = 0; i < word.Length; i++) | |
{ | |
int num = word[i] - 48; | |
if ((num & 1) == 1) | |
{ | |
int left = (i - 1) > -1 ? | |
word[i - 1] - 48 : 0; | |
int right = (i + 1) < word.Length ? | |
word[i + 1] - 48 : 0; | |
int sum = left + right + num; | |
text = text + sum; | |
} | |
else | |
{ | |
text = text + (num * num); | |
} | |
} | |
return text; | |
} | |
private static string ConvertMessage(string word) | |
{ | |
string number = ""; | |
for (int i = 0; i < word.Length; i++) | |
{ | |
int num = word[i] % 10; | |
number = number + num; | |
} | |
return number; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment