Advertisement
StreetKatya

16

Oct 31st, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 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 lab3_2_16
  8. {
  9. /*
  10. * Дан целочисленный массив размера n.
  11. * Преобразовать массив, увеличив все его серии наименьшей длины на один элемент.
  12. */
  13. internal class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. int[] array, indexLastElement;
  18. int[] resultArray;
  19. int n, countSeries = 0;
  20. Console.WriteLine("Введите размерность массива:");
  21. n = int.Parse(Console.ReadLine());
  22.  
  23. int minLenght = int.MaxValue, tempMinLenght = 1;
  24.  
  25. if (n > 0)
  26. {
  27. Console.WriteLine($"Введите {n} элементов массива");
  28. array = new int[n];
  29. indexLastElement = new int[n];
  30.  
  31. for (int i = 0; i < n; i++)
  32. {
  33. array[i] = int.Parse(Console.ReadLine());
  34. }
  35. for (int i = 1; i < n; i++)
  36. {
  37. if (array[i] == array[i - 1])
  38. {
  39. tempMinLenght++;
  40. //if((i == n-1) && (minLenght == tempMinLenght || tempMinLenght< minLenght))
  41. //{
  42. // indexLastElement[countSeries] = i - 1;
  43. //}
  44. }
  45. else
  46. {
  47. if (tempMinLenght < minLenght)
  48. {
  49. minLenght = tempMinLenght;
  50. countSeries = 1;
  51. indexLastElement = new int[n];
  52. indexLastElement[0] = i - 1;
  53.  
  54. }
  55. else if (tempMinLenght == minLenght)
  56. {
  57. countSeries++;
  58. indexLastElement[countSeries-1] = i - 1;
  59. }
  60. tempMinLenght = 1;
  61. }
  62. }
  63. if (tempMinLenght != 0)
  64. {
  65. if (tempMinLenght < minLenght)
  66. {
  67. minLenght = tempMinLenght;
  68. countSeries = 0;
  69. indexLastElement = new int[n];
  70. indexLastElement[0] = n - 1;
  71.  
  72. }
  73. else if (tempMinLenght == minLenght)
  74. {
  75. indexLastElement[countSeries] = n - 1;
  76. countSeries++;
  77.  
  78. }
  79. }
  80. resultArray = new int[n + countSeries];
  81. //int j = 0;//индекс по массиву индексов
  82. //for(int i = 0; i < n + countSeries; i++)
  83. //{
  84. // if(indexLastElement[j] == i-j)
  85. // {
  86. // int tempElement = array[indexLastElement[j]];
  87. // for(int k = 0; k <= minLenght; k++)
  88. // {
  89. // resultArray[i+k] = tempElement;
  90. // }
  91. // //resultArray[i+1] = array[indexLastElement[j]];
  92. // i ++;
  93. // j++;
  94. // }
  95. // resultArray[i] = array[i];
  96. //}
  97. for (int i = 1; i < indexLastElement.Count(); i++)
  98. {
  99. if(indexLastElement[i]==0) { indexLastElement[i] = -1; }
  100. }
  101. //int j = 0;
  102.  
  103. for (int i = 0; i < n + countSeries; i++)
  104. {
  105. if (!indexLastElement.Contains(j)) { resultArray[j] = array[j]; /*j++;*/ }
  106. else { resultArray[i] = array[j]; resultArray[i + 1] = resultArray[i]; i++; /*j++;*/ }
  107. }
  108.  
  109.  
  110. Console.WriteLine("Новый массив");
  111. for (int i = 0; i < n + countSeries; i++) // Печатаем массив
  112. {
  113. Console.Write(resultArray[i] + " ");
  114.  
  115. }
  116. }
  117. else Console.WriteLine("Массив состоит из 0 элементов");
  118.  
  119. Console.ReadKey();
  120.  
  121. }
  122. }
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement