Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace Ekz1
- {
- class Program
- {
- static Random rand = new Random();
- static void CountEach(ref char[] arr)
- {
- int i = 1;
- int numb = 1, j = 0, last = 0;
- char[] str = new char[arr.Length];
- Array.Copy(arr, str, arr.Length);
- Array.Sort(str);
- char letter = str[0];
- for (i = 0; i < str.Length; ++i)
- {
- if (str[i] != letter)
- {
- letter = str[i];
- ++numb;
- }
- }
- char[] letters = new char[numb];
- int[] count = new int[numb];
- letter = str[0];
- Console.WriteLine($"Количество встреч каждой буквы:");
- for (i = 0; i < str.Length; ++i)
- {
- if (str[i] != letter)
- {
- letter = str[i];
- letters[j + 1] = str[i];
- count[j++] = i - last;
- last = i;
- Console.Write($"{str[i]} встречено {count[j - 1]}");
- if (count[j - 1] % 10 > 1 && count[j - 1] % 10 < 5)
- {
- if (count[j - 1] > 10 && count[j - 1] < 20)
- {
- Console.WriteLine(" раз.");
- } else
- {
- Console.WriteLine(" раза.");
- }
- } else
- {
- Console.WriteLine(" раз.");
- }
- }
- }
- }
- /*
- b встречено 354 раза.
- c встречено 369 раза.
- d встречено 368 раза.
- e встречено 391 раза.
- f встречено 375 раза.
- g встречено 389 раза.
- h встречено 406 раза.
- i встречено 394 раза.
- j встречено 390 раза.
- k встречено 374 раза.
- l встречено 404 раза.
- m встречено 393 раза.
- n встречено 387 раза.
- o встречено 390 раза.
- p встречено 385 раза.
- q встречено 382 раза.
- r встречено 385 раза.
- s встречено 375 раза.
- t встречено 423 раза.
- u встречено 363 раза.
- v встречено 390 раза.
- w встречено 369 раза.
- x встречено 366 раза.
- y встречено 367 раза.*/
- static void ChInterval(char ch1, char ch2, ref char[] arr)
- {
- Console.Write($"Выбрано от {ch1} до {ch2}: ");
- try
- {
- for (int i = 0; i < arr.Length; ++i)
- {
- if (arr[i] > ch1 && arr[i] < ch2)
- {
- Console.Write($"{arr[i]} ");
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- static void GoodForK(int k, ref char[] arr)
- {
- try
- {
- Console.WriteLine();
- Console.Write($"Кратные {k} элементы: ");
- for (int i = 0; i < arr.Length; ++i)
- {
- if ((arr[i] - 97) % k == 0)
- {
- Console.Write($"{arr[i]} ");
- }
- }
- Console.WriteLine();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- static void GenerateArray(ref char[] arr)
- {
- Console.Write($"Сгенерированный массив длины {arr.Length}: ");
- for (int i = 0; i < arr.Length; ++i)
- {
- arr[i] = (char)rand.Next(97, 123);
- Console.Write($"{arr[i]} ");
- }
- Console.WriteLine();
- }
- static void MostFrequent(char[] arr)
- {
- try
- {
- int i = 1;
- int numb = 1, res = 1, j = 0, last = 0;
- char[] str = new char[arr.Length];
- Array.Copy(arr, str, arr.Length);
- Array.Sort(str);
- Console.Write($"Sorted: ");
- Console.WriteLine(str);
- char letter = str[0];
- for (i = 0; i < str.Length; ++i)
- {
- if (str[i] != letter)
- {
- letter = str[i];
- ++numb;
- }
- }
- char[] letters = new char[numb];
- int[] count = new int[numb];
- letter = str[0];
- for (i = 0; i < str.Length; ++i)
- {
- if (str[i] != letter)
- {
- letter = str[i];
- letters[j + 1] = str[i];
- count[j++] = i - last;
- last = i;
- }
- }
- count[j] = i - last;
- for (i = 0; i < count.Length; ++i)
- {
- if (count[i] > res)
- {
- res = count[i];
- }
- }
- for (i = 0; i < count.Length; ++i)
- {
- if (count[i] == res)
- {
- ++numb;
- }
- }
- numb = 0;
- bool next = false;
- for (i = 0; i < count.Length; ++i)
- {
- if (count[i] == res)
- {
- if (numb > 1)
- {
- if (next == true)
- {
- Console.Write($", {letters[i + 1]}");
- }
- else
- {
- Console.Write($"Чаще всего встречаются {letters[i + 1]}");
- next = true;
- }
- }
- else
- {
- Console.Write($"Чаще всего встречается {letters[i + 1]}");
- }
- }
- }
- Console.Write(".");
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- static void Main(string[] args)
- {
- try
- {
- int n, k;
- char ch1, ch2;
- do
- {
- Console.Write("Введите n: ");
- while (!int.TryParse(Console.ReadLine(), out n) || n < 1)
- {
- Console.WriteLine("Некорректный ввод! Повторите попытку.");
- Console.Write("Введите n: ");
- }
- Console.Write("Введите k: ");
- while (!int.TryParse(Console.ReadLine(), out k))
- {
- Console.WriteLine("Некорректный ввод! Повторите попытку.");
- Console.Write("Введите k: ");
- }
- Console.Write("Введите ch1: ");
- while (!char.TryParse(Console.ReadLine(), out ch1) || ch1 < 97 || ch1 >= 122)
- {
- Console.WriteLine("Некорректный ввод! Повторите попытку.");
- Console.Write("Введите ch1: ");
- }
- Console.Write("Введите ch2: ");
- while (!char.TryParse(Console.ReadLine(), out ch2) || ch2 - ch1 - 1 <= 0 || ch2 <= 97 || ch2 > 122)
- {
- Console.WriteLine("Некорректный ввод! Повторите попытку.");
- Console.Write("Введите ch2: ");
- }
- char[] arr = new char[n];
- GenerateArray(ref arr);
- MostFrequent(arr);
- GoodForK(k, ref arr);
- CountEach(ref arr);
- ChInterval(ch1, ch2, ref arr);
- Console.WriteLine();
- Console.WriteLine($"Нажмите Escape, чтобы выйти.");
- } while (Console.ReadKey(intercept: true).Key != ConsoleKey.Escape);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement