Advertisement
zh_stoqnov

Encrypted Matrix

Mar 30th, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 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 Problem_4
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string msg = Console.ReadLine();
  14. char dir = char.Parse(Console.ReadLine());
  15.  
  16. string number = convertToNumber(msg);
  17. string num = "";
  18. int firstDigit = Convert.ToInt32(new string(number[0], 1));
  19. int lastDigit = 0;
  20. int secondDigit = 0;
  21. int beforeLastDigit = 0;
  22. if (convertToNumber(msg).Length > 1)
  23. {
  24. lastDigit = Convert.ToInt32(new string(number[number.Length - 1], 1));
  25. secondDigit = Convert.ToInt32(new string(number[1], 1));
  26. beforeLastDigit = Convert.ToInt32(new string(number[number.Length - 2], 1));
  27. }
  28.  
  29. if (firstDigit == 0 || firstDigit % 2 == 0)
  30. {
  31. firstDigit = firstDigit * firstDigit;
  32. }
  33. if (firstDigit % 2 != 0)
  34. {
  35. firstDigit = firstDigit + 0 + secondDigit;
  36. }
  37. if (lastDigit == 0 || lastDigit % 2 == 0)
  38. {
  39. lastDigit = lastDigit * lastDigit;
  40. }
  41. if (lastDigit % 2 != 0)
  42. {
  43. lastDigit = lastDigit + 0 + beforeLastDigit;
  44. }
  45. num += firstDigit;
  46. for (int i = 1; i < number.Length - 1; i++)
  47. {
  48. int digit = Convert.ToInt32(new string(number[i], 1));
  49. int beforeDigit = Convert.ToInt32(new string(number[i - 1], 1));
  50. int afterDigit = Convert.ToInt32(new string(number[i + 1], 1));
  51. if (digit == 0 || digit % 2 == 0)
  52. {
  53. digit = digit * digit;
  54. }
  55. if (digit % 2 != 0)
  56. {
  57. digit = digit + beforeDigit + afterDigit;
  58. }
  59. num += digit;
  60. }
  61. num += lastDigit;
  62. int len = num.Length;
  63. char[,] matrix = new char[len, len];
  64.  
  65. int count = 0;
  66. for (int i = 0; i < len; i++)
  67. {
  68. for (int j = 0; j < len; j++)
  69. {
  70. matrix[i, j] = '0';
  71. }
  72. }
  73.  
  74. if (dir == '\\')
  75. {
  76. for (int i = 0; i < len; i++)
  77. {
  78. for (int j = 0; j < len; j++)
  79. {
  80. matrix[i, count] = num[count];
  81. }
  82. count++;
  83. }
  84. }
  85. else
  86. {
  87. for (int i = len - 1; i >= 0; i--)
  88. {
  89. for (int j = 0; j < len; j++)
  90. {
  91. matrix[i, count] = num[count];
  92. }
  93. count++;
  94. }
  95. }
  96.  
  97. if (convertToNumber(msg).Length == 1)
  98. {
  99. Console.WriteLine(convertToNumber(msg));
  100. }
  101. else
  102. {
  103. for (int i = 0; i < len; i++)
  104. {
  105. for (int j = 0; j < len; j++)
  106. {
  107. Console.Write(matrix[i, j] + " ");
  108. }
  109. Console.WriteLine();
  110. }
  111. }
  112.  
  113. }
  114.  
  115. static string convertToNumber(string msg)
  116. {
  117. string output = "";
  118. for (int i = 0; i < msg.Length; i++)
  119. {
  120. int last = getValue(msg[i]) % 10;
  121. output += last;
  122. }
  123. return output;
  124. }
  125. static int getValue(char c)
  126. {
  127. return (int)c;
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement