PhotoShaman

ПР #2

May 12th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 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 PR2
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Random rnd = new Random();
  14.             Console.Write("Введите размерность массива: ");
  15.             int length = InputNumber(1000);
  16.  
  17.             List<int> array = new List<int>();
  18.             Console.Write("Укажите диапазон рандома:\nМинимум: ");
  19.             int minValue = InputNumber(1000, -1000);
  20.             Console.Write("Максимум: ");
  21.             int maxValue = InputNumber(1000, -1000);
  22.  
  23.             int negativDob = 1, maxIndex = 0, sumPosEl = 0;
  24.             for (int i = 0; i < length; i++)
  25.             {
  26.                 array.Add(rnd.Next(minValue, maxValue));
  27.                 if (array[i] < 0)
  28.                 {
  29.                     negativDob *= array[i];
  30.                 }
  31.                 if (array[maxIndex] < array[i])
  32.                 {
  33.                     maxIndex = i;
  34.                 }
  35.                 Console.Write(array[i] + "\t");
  36.             }
  37.             Console.WriteLine("\nПроизведение отрицательных элементов: " + negativDob);
  38.             for (int i = 0; i < maxIndex; i++)
  39.             {
  40.                 if (array[i] >= 0)
  41.                 {
  42.                     sumPosEl += array[i];
  43.                 }
  44.             }
  45.  
  46.             Console.WriteLine("Сума положительных елементов массива перед максимальным элементом: " + sumPosEl);
  47.  
  48.             for (int i = 0; i < length; i++)
  49.             {
  50.                 bool isDublicate = false;
  51.                 for (int j = i + 1; j < array.Count; j++)
  52.                 {
  53.                     if (array[i] == array[j])
  54.                     {
  55.                         if (!isDublicate)
  56.                         {
  57.                             isDublicate = true;
  58.                         }
  59.                         else
  60.                         {
  61.                             array.RemoveAt(j);
  62.                             j--;
  63.                         }
  64.                     }
  65.                 }
  66.             } /*Code protected by ZAV*/
  67.             Console.WriteLine("\nУдалены элементы, повторяющиеся больше двух раз:\n");
  68.             for (int i = 0; i < array.Count; i++)
  69.             {
  70.                 Console.Write(array[i] + "\t");
  71.             }
  72.             //-----3
  73.             for (int i = 0; i < array.Count; i++)
  74.             {
  75.                 int count = 0;
  76.                 if (array[i] == 0)
  77.                 {
  78.                     array.Insert(count++, array[i]);
  79.                     array.RemoveAt(i + 1);
  80.                 }
  81.             }
  82.             Console.WriteLine("\nЗадание 3:");
  83.             for (int i = 0; i < array.Count; i++)
  84.             {
  85.                 Console.Write(array[i] + "\t");
  86.             }
  87.             //-----4
  88.  
  89.             int countEven = 0;
  90.             for (int i = 0; i < array.Count; i++)
  91.             {
  92.                 if (array[i] % 2 == 0)
  93.                 {
  94.                     array.Insert(countEven++, array[i]);
  95.                     array.RemoveAt(i + 1);
  96.                 }
  97.             }
  98.             array.Sort(0, countEven, null);
  99.             array.Sort(countEven, array.Count - countEven, Comparer<int>.Create((i1, i2) => -i1.CompareTo(i2)));
  100.             Console.WriteLine("\nЗадание 4:");
  101.             for (int i = 0; i < array.Count; i++)
  102.             {
  103.                 Console.Write(array[i] + "\t");
  104.             }
  105.  
  106.             Console.ReadLine();
  107.         }
  108.  
  109.         static int InputNumber(int max = 0x7FFFFFFF, int min = 0)
  110.         {
  111.             int number;
  112.             while (!Int32.TryParse(Console.ReadLine(), out number) || number < min || number > max)
  113.             {
  114.                 Console.WriteLine("Ошибка ввода. Повторите попытку.");
  115.             }
  116.             return number;
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment