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 PR2
- {
- class Program
- {
- static void Main(string[] args)
- {
- Random rnd = new Random();
- Console.Write("Введите размерность массива: ");
- int length = InputNumber(1000);
- List<int> array = new List<int>();
- Console.Write("Укажите диапазон рандома:\nМинимум: ");
- int minValue = InputNumber(1000, -1000);
- Console.Write("Максимум: ");
- int maxValue = InputNumber(1000, -1000);
- int negativDob = 1, maxIndex = 0, sumPosEl = 0;
- for (int i = 0; i < length; i++)
- {
- array.Add(rnd.Next(minValue, maxValue));
- if (array[i] < 0)
- {
- negativDob *= array[i];
- }
- if (array[maxIndex] < array[i])
- {
- maxIndex = i;
- }
- Console.Write(array[i] + "\t");
- }
- Console.WriteLine("\nПроизведение отрицательных элементов: " + negativDob);
- for (int i = 0; i < maxIndex; i++)
- {
- if (array[i] >= 0)
- {
- sumPosEl += array[i];
- }
- }
- Console.WriteLine("Сума положительных елементов массива перед максимальным элементом: " + sumPosEl);
- for (int i = 0; i < length; i++)
- {
- bool isDublicate = false;
- for (int j = i + 1; j < array.Count; j++)
- {
- if (array[i] == array[j])
- {
- if (!isDublicate)
- {
- isDublicate = true;
- }
- else
- {
- array.RemoveAt(j);
- j--;
- }
- }
- }
- } /*Code protected by ZAV*/
- Console.WriteLine("\nУдалены элементы, повторяющиеся больше двух раз:\n");
- for (int i = 0; i < array.Count; i++)
- {
- Console.Write(array[i] + "\t");
- }
- //-----3
- for (int i = 0; i < array.Count; i++)
- {
- int count = 0;
- if (array[i] == 0)
- {
- array.Insert(count++, array[i]);
- array.RemoveAt(i + 1);
- }
- }
- Console.WriteLine("\nЗадание 3:");
- for (int i = 0; i < array.Count; i++)
- {
- Console.Write(array[i] + "\t");
- }
- //-----4
- int countEven = 0;
- for (int i = 0; i < array.Count; i++)
- {
- if (array[i] % 2 == 0)
- {
- array.Insert(countEven++, array[i]);
- array.RemoveAt(i + 1);
- }
- }
- array.Sort(0, countEven, null);
- array.Sort(countEven, array.Count - countEven, Comparer<int>.Create((i1, i2) => -i1.CompareTo(i2)));
- Console.WriteLine("\nЗадание 4:");
- for (int i = 0; i < array.Count; i++)
- {
- Console.Write(array[i] + "\t");
- }
- Console.ReadLine();
- }
- static int InputNumber(int max = 0x7FFFFFFF, int min = 0)
- {
- int number;
- while (!Int32.TryParse(Console.ReadLine(), out number) || number < min || number > max)
- {
- Console.WriteLine("Ошибка ввода. Повторите попытку.");
- }
- return number;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment