Advertisement
Guest User

4. Encrypted Matrix

a guest
Mar 30th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication4
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string message = Console.ReadLine();
  14. string direction = Console.ReadLine();
  15.  
  16. string convertedMessage = "";
  17. for (int i = 0; i < message.Length; i++)
  18. {
  19. char c = message[i];
  20. byte[] bits = Encoding.ASCII.GetBytes(c.ToString());
  21. String asciiNum = "";
  22. for (int b = 0; b < bits.Length; b++)
  23. {
  24. asciiNum += bits[b].ToString();
  25. }
  26. convertedMessage += asciiNum[asciiNum.Length - 1];
  27. }
  28.  
  29. //Console.WriteLine(convertedMessage);
  30. string result = "";
  31. for (int i = 0; i < convertedMessage.Length; i++)
  32. {
  33. int digit = int.Parse(convertedMessage[i].ToString());
  34. if (digit % 2 == 0 || digit == 0)
  35. {
  36. digit *= digit;
  37. }
  38. else
  39. {
  40. int previosIndex = i - 1;
  41. int nextIndex = i + 1;
  42. int prevNum = 0;
  43. int nextNum = 0;
  44. if (previosIndex < 0)
  45. {
  46. prevNum = 0;
  47. }
  48. else
  49. {
  50. prevNum = int.Parse(convertedMessage[previosIndex].ToString());
  51. }
  52.  
  53. if (nextIndex > convertedMessage.Length - 1)
  54. {
  55. nextNum = 0;
  56. }
  57. else
  58. {
  59. nextNum = int.Parse(convertedMessage[nextIndex].ToString());
  60. }
  61.  
  62. digit += prevNum;
  63. digit += nextNum;
  64. }
  65. result += digit.ToString();
  66. }
  67.  
  68. //Console.WriteLine(result);
  69. int height = result.Length;
  70. int width = result.Length;
  71. if (direction == "\\")
  72. {
  73. int colIndex = 0;
  74. int numIndex = 0;
  75. for (int row = 0; row < height; row++)
  76. {
  77. for (int col = 0; col < width; col++)
  78. {
  79. if (col == colIndex)
  80. {
  81. Console.Write(result[numIndex]);
  82. }
  83. else
  84. {
  85. Console.Write("0");
  86. }
  87.  
  88. if (col < width - 1)
  89. {
  90. Console.Write(" ");
  91. }
  92. }
  93. colIndex++;
  94. numIndex++;
  95. Console.WriteLine();
  96. }
  97. }
  98. else
  99. {
  100. int colIndex = width - 1;
  101. int numIndex = result.Length - 1;
  102. for (int row = 0; row < height; row++)
  103. {
  104. for (int col = 0; col < width; col++)
  105. {
  106. if (col == colIndex)
  107. {
  108. Console.Write(result[numIndex]);
  109. }
  110. else
  111. {
  112. Console.Write("0");
  113. }
  114. if (col < width - 1)
  115. {
  116. Console.Write(" ");
  117. }
  118. }
  119. colIndex--;
  120. numIndex--;
  121. Console.WriteLine();
  122. }
  123. }
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement