Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace KR
  5. {
  6. class Program
  7. {
  8. static Random rnd = new Random();
  9. static string MakeRndString(int N)
  10. {
  11. char[] str = new char[N];
  12. for (int i = 0; i < N; i++)
  13. {
  14. str[i] = (char)rnd.Next(97, 123);
  15. }
  16. return string.Join("", str);
  17. }
  18. static char MostFrequentSearcher(char[] arr)
  19. {
  20. char[] buff = arr;
  21. Array.Sort(buff);
  22. int max = 1, counter = 0;
  23. char count = buff[0], maxChar = buff[0];
  24.  
  25. for (int i = 0; i < buff.Length; i++)
  26. {
  27. if (count == buff[i])
  28. {
  29. counter++;
  30. continue;
  31. }
  32. count = buff[i];
  33. if (max < counter)
  34. {
  35. maxChar = buff[i - 1];
  36. max = counter;
  37. }
  38. }
  39. return maxChar;
  40. }
  41. static char[] MakeNewArr(char[] arr)
  42. {
  43. char[] buff = arr;
  44. //В разработке
  45. return buff;
  46. }
  47. static bool Continue(string pass)
  48. {
  49. Console.WriteLine($"Хотите продолжить, введите: [ {pass} ]\n");
  50. return Console.ReadLine() == pass;
  51. }
  52. static bool Continue(ConsoleKey pass)
  53. {
  54. Console.WriteLine($"Хотите продолжить, нажмите: [ {pass.ToString()} ]\n");
  55. return Console.ReadKey().Key == pass;
  56. }
  57. static void ReadInt(out int num, int min, int max)
  58. {
  59. while (!int.TryParse(Console.ReadLine(), out num)
  60. || num > max || num < min)
  61. {
  62. if (num > max)
  63. {
  64. Console.WriteLine("Слишком большое число. Давай заново");
  65. }
  66. else if (num < min)
  67. {
  68. Console.WriteLine("Слишком маленькое число. Давай заново");
  69. }
  70. else
  71. {
  72. Console.WriteLine("Это и близко не int. Давай заново");
  73. }
  74. }
  75. }
  76. static void Main(string[] args)
  77. {
  78. do
  79. {
  80. int N;
  81. Console.WriteLine($"Введите число в промежутке (0; +бесконечность)");
  82. ReadInt(out N, 1, int.MaxValue);
  83. string str = MakeRndString(N);
  84. Console.WriteLine($"Сгенерированная строка:\t\t{str}");
  85.  
  86. Console.Write($"Полученный массив символов:\t");
  87. char[] chArr = str.ToCharArray();
  88. foreach (var item in chArr)
  89. {
  90. Console.Write(item + " ");
  91. }
  92. Console.WriteLine($"\nСамый частый элемент:\t\t{MostFrequentSearcher(chArr)}\n");
  93.  
  94.  
  95. } while (Continue(ConsoleKey.Enter));
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement