Advertisement
JulianJulianov

11. Encrypt, Sort and Print Array

Feb 9th, 2020
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. 01. Encrypt, Sort and Print Array
  2. Write a program that reads a sequence of strings from the console. Encrypt every string by summing:
  3. • The code of each vowel multiplied by the string length
  4. • The code of each consonant divided by the string length
  5. Sort the number sequence in ascending order and print it on the console.
  6. On first line, you will always receive the number of strings you have to read.
  7. Examples
  8. Input Output Comments
  9. 4 1071 Peter = 1071
  10. Peter 1168 Maria = 1532
  11. Maria 1532 Katya = 1613
  12. Katya 1613 Todor = 1168
  13. Todor
  14.  
  15. 3
  16. Sofia 1396 Sofia = 1601
  17. London 1601 London = 1396
  18. Washington 3202 Washington = 3202
  19.  
  20.  
  21. using System;
  22. using System.Linq;
  23.  
  24. namespace _01EncryptSortAndPrintArray
  25. {
  26. class Program
  27. {
  28. static void Main(string[] args)
  29. {
  30. var number = int.Parse(Console.ReadLine());
  31.  
  32. int sumWord = 0;
  33. string sumAllWord = "";
  34. for (int a = 0; a < number; a++)
  35. {
  36. string word = Console.ReadLine();
  37. int sumVowel = 0;
  38. int sumConsonant = 0;
  39. int counter = 0;
  40.  
  41. foreach (char item in word)
  42. {
  43. if (item == 'A' || item == 'a' || item == 'E' || item == 'e' || item == 'I' || item == 'i' || item == 'O' || item == 'o' || item == 'U' || item == 'u' || item == 'Y' || item == 'y')
  44. { // "Y" e в 97.5% от случаите гласна и само 2.5% съгласна!)
  45. int position = word[counter];
  46. int vowelMultiplied = position * word.Length;
  47. sumVowel += vowelMultiplied;
  48. counter++;
  49. }
  50. else
  51. {
  52. int position = word[counter];
  53. int consonantDivided = position / word.Length;
  54. sumConsonant += consonantDivided;
  55. counter++;
  56. }
  57. }
  58. sumWord = sumVowel + sumConsonant;
  59. sumAllWord += sumWord + " ";
  60. }
  61. int[] sumAll = sumAllWord.Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  62. using System;
  63. using System.Linq;
  64.  
  65. namespace _01EncryptSortAndPrintArray
  66. {
  67. class Program
  68. {
  69. static void Main(string[] args)
  70. {
  71. var number = int.Parse(Console.ReadLine());
  72.  
  73. int sumWord = 0;
  74. string sumAllWord = "";
  75. for (int a = 0; a < number; a++)
  76. {
  77. string word = Console.ReadLine();
  78. int sumVowel = 0;
  79. int sumConsonant = 0;
  80. int counter = 0;
  81.  
  82. foreach (char item in word)
  83. {
  84. if (item == 'a' || item == 'e' || item == 'i' || item == 'o' || item == 'u' || item == 'y')
  85. { // "Y" e в 97.5% от случаите гласна и само 2.5% съгласна!)
  86. int position = word[counter];
  87. int vowelMultiplied = position * word.Length;
  88. sumVowel += vowelMultiplied;
  89. counter++;
  90. }
  91. else
  92. {
  93. int position = word[counter];
  94. int consonantDivided = position / word.Length;
  95. sumConsonant += consonantDivided;
  96. counter++;
  97. }
  98. }
  99. sumWord = sumVowel + sumConsonant;
  100. sumAllWord += sumWord + " ";
  101. }
  102. int[] sumAll = sumAllWord.Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  103. bool swapped = false;
  104. do //Сортира от най-малкото към най-голямото число! За обратния ефект трябва да се разменят[i-1]с[i]!
  105. {
  106. swapped = false;
  107. for (int i = 1; i < sumAll.Length; i++)
  108. {
  109. int leftElement = sumAll[i - 1];
  110. int rightElement = sumAll[i];
  111. if (leftElement > rightElement)
  112. {
  113. sumAll[i - 1] = rightElement;
  114. sumAll[i] = leftElement;
  115. swapped = true;
  116. }
  117. }
  118. } while (swapped);
  119. Console.WriteLine(string.Join("\n", sumAll));
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement