lolblach333

Untitled

Apr 13th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 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.  
  8. namespace LocalMaximums
  9. {
  10. class Program
  11. {
  12. private const string
  13. InitialArray = "Начальный массив:",
  14. LocalMaximum = "Локальные максимумы выделены красным цветом:",
  15. Separator = " ";
  16. private const int
  17. Min = 0, Max = 20, ArrayLength = 30;
  18.  
  19. static void Main(string[] args)
  20. {
  21. int[] numbers = GenerateRandomNumbers(Min, Max);
  22. PrintMaxinmums(numbers, FindMaximumsIndices(numbers));
  23. }
  24.  
  25. static int[] GenerateRandomNumbers(int min, int max)
  26. {
  27. int[] numbers = new int[ArrayLength];
  28. Random random = new Random();
  29.  
  30. for (int i = 0; i < ArrayLength; i++)
  31. {
  32. numbers[i] = random.Next(min, max);
  33. }
  34.  
  35. return numbers;
  36. }
  37.  
  38. static int[] FindMaximumsIndices(int[] numbers)
  39. {
  40. int[] indices = new int[ArrayLength];
  41.  
  42. for (int i = 0; i < ArrayLength; i++)
  43. {
  44. switch (i)
  45. {
  46. case 0:
  47. {
  48. indices[i] = numbers[i + 1] < numbers[i] ? 1 : 0;
  49. break;
  50. }
  51. case ArrayLength - 1:
  52. {
  53. indices[i] = numbers[i - 1] < numbers[i] ? 1 : 0;
  54. break;
  55. }
  56. default:
  57. {
  58. indices[i] = numbers[i - 1] < numbers[i] && numbers[i + 1] < numbers[i] ? 1 : 0;
  59. break;
  60. }
  61. }
  62. }
  63.  
  64. return indices;
  65. }
  66.  
  67. static void PrintMaxinmums(int[] initialArray, int[] maximums)
  68. {
  69. Console.WriteLine(InitialArray);
  70. Console.WriteLine(string.Join(Separator, initialArray));
  71.  
  72. Console.WriteLine(LocalMaximum);
  73.  
  74. for (int i = 0; i < ArrayLength; i++)
  75. {
  76. Console.ForegroundColor = maximums[i] == 0 ? ConsoleColor.Gray : ConsoleColor.Red;
  77. Console.Write(initialArray[i] + Separator);
  78. }
  79.  
  80. Console.ReadKey();
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment