Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace lab3_2_16
- {
- /*
- * Дан целочисленный массив размера n.
- * Преобразовать массив, увеличив все его серии наименьшей длины на один элемент.
- */
- internal class Program
- {
- static void Main(string[] args)
- {
- int[] array, indexLastElement;
- int[] resultArray;
- int n, countSeries = 0;
- Console.WriteLine("Введите размерность массива:");
- n = int.Parse(Console.ReadLine());
- int minLenght = int.MaxValue, tempMinLenght = 1;
- if (n > 0)
- {
- Console.WriteLine($"Введите {n} элементов массива");
- array = new int[n];
- indexLastElement = new int[n];
- for (int i = 0; i < n; i++)
- {
- array[i] = int.Parse(Console.ReadLine());
- }
- for (int i = 1; i < n; i++)
- {
- if (array[i] == array[i - 1])
- {
- tempMinLenght++;
- //if((i == n-1) && (minLenght == tempMinLenght || tempMinLenght< minLenght))
- //{
- // indexLastElement[countSeries] = i - 1;
- //}
- }
- else
- {
- if (tempMinLenght < minLenght)
- {
- minLenght = tempMinLenght;
- countSeries = 1;
- indexLastElement = new int[n];
- indexLastElement[0] = i - 1;
- }
- else if (tempMinLenght == minLenght)
- {
- countSeries++;
- indexLastElement[countSeries-1] = i - 1;
- }
- tempMinLenght = 1;
- }
- }
- if (tempMinLenght != 0)
- {
- if (tempMinLenght < minLenght)
- {
- minLenght = tempMinLenght;
- countSeries = 0;
- indexLastElement = new int[n];
- indexLastElement[0] = n - 1;
- }
- else if (tempMinLenght == minLenght)
- {
- indexLastElement[countSeries] = n - 1;
- countSeries++;
- }
- }
- resultArray = new int[n + countSeries];
- //int j = 0;//индекс по массиву индексов
- //for(int i = 0; i < n + countSeries; i++)
- //{
- // if(indexLastElement[j] == i-j)
- // {
- // int tempElement = array[indexLastElement[j]];
- // for(int k = 0; k <= minLenght; k++)
- // {
- // resultArray[i+k] = tempElement;
- // }
- // //resultArray[i+1] = array[indexLastElement[j]];
- // i ++;
- // j++;
- // }
- // resultArray[i] = array[i];
- //}
- for (int i = 1; i < indexLastElement.Count(); i++)
- {
- if(indexLastElement[i]==0) { indexLastElement[i] = -1; }
- }
- //int j = 0;
- for (int i = 0; i < n + countSeries; i++)
- {
- if (!indexLastElement.Contains(j)) { resultArray[j] = array[j]; /*j++;*/ }
- else { resultArray[i] = array[j]; resultArray[i + 1] = resultArray[i]; i++; /*j++;*/ }
- }
- Console.WriteLine("Новый массив");
- for (int i = 0; i < n + countSeries; i++) // Печатаем массив
- {
- Console.Write(resultArray[i] + " ");
- }
- }
- else Console.WriteLine("Массив состоит из 0 элементов");
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement