Advertisement
Achek

Task1 (13.11.2019)

Nov 12th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 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. using System.IO;
  7.  
  8. namespace CasualOntario
  9. {
  10.     class Program
  11.     {
  12.         static ConsoleKey keyToClose = ConsoleKey.Escape;
  13.         static Random rnd = new Random();
  14.  
  15.         static bool ToClose()
  16.         {
  17.             return Console.ReadKey().Key == keyToClose;
  18.         }
  19.  
  20.         // Ввод size массива с клавиатуры.
  21.         static bool EnterArraySize(ref int arrSize)
  22.         {
  23.             Console.WriteLine("Введите кол-во элементов массива (Целое положительное число int)...");
  24.             return (int.TryParse(Console.ReadLine(), out arrSize));
  25.         }
  26.  
  27.         // Создание нового одномерного массива размерностью от 0 до 16.
  28.         static int[] NewArrayGenerate(ref int countElem)
  29.         {
  30.             int[] arr = new int[rnd.Next(0, 16)];
  31.             for (int i = 0; i < arr.Length; i++)
  32.                 arr[i] = rnd.Next(0, 1000);
  33.             countElem += (arr.Length + 1) / 2;
  34.             return arr;
  35.         }
  36.  
  37.         // Создание корневого массива
  38.         static int[][] FirstCernelArrayGenerate(ref int countElem)
  39.         {
  40.             int arrSize = 0;
  41.             while (!EnterArraySize(ref arrSize))
  42.                 Console.WriteLine("Некорректный ввод данных, try еще раз...");
  43.  
  44.             int[][] cernelArr = new int[arrSize][];
  45.             for (int i = 0; i < arrSize; i++)
  46.                 cernelArr[i] = NewArrayGenerate(ref countElem);
  47.             return cernelArr;
  48.         }
  49.        
  50.         static int[] SecondArrayGenerate(int countElem, int[][] firstArr)
  51.         {
  52.             int[] arr = new int[countElem];
  53.             int ind = 0;
  54.             for (int i = 0; i < firstArr.Length; i++)
  55.                 for (int j = 0; j < firstArr[i].Length; j++)
  56.                     if (j % 2 == 0)
  57.                     {
  58.                         arr[ind] = firstArr[i][j];
  59.                         ind++;
  60.                     }
  61.             return arr;
  62.         }
  63.  
  64.         // Вывод массива массивов.
  65.         static void ShowFirstArray(int[][] arr)
  66.         {
  67.             Console.WriteLine("Массив A");
  68.             for (int i = 0; i < arr.Length; i++)
  69.             {
  70.                 Console.Write("{0} (size = {1}) : ", i + 1, arr[i].Length);
  71.                 for (int j = 0; j < arr[i].Length; j++)
  72.                     Console.Write(arr[i][j] + " ");
  73.                 Console.WriteLine();
  74.             }
  75.         }
  76.  
  77.         static void ShowSecondArray(int[] secondArr)
  78.         {
  79.             Console.WriteLine("Массив B");
  80.             for (int i = 0; i < secondArr.Length; ++i)
  81.                 Console.Write(secondArr[i] + " ");
  82.         }
  83.  
  84.         static void Main(string[] args)
  85.         {
  86.             do
  87.             {
  88.                 int countElem = 0;
  89.                 int[][] cernelArray = FirstCernelArrayGenerate(ref countElem);
  90.                 ShowFirstArray(cernelArray);
  91.                 int[] secondArr = SecondArrayGenerate(countElem, cernelArray);
  92.                 ShowSecondArray(secondArr);
  93.                 Console.WriteLine("Чтобы закончить работу программы нажмите Escape...");
  94.             } while (!ToClose());
  95.             Console.WriteLine("Программа завершила работу...");
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement