Advertisement
Guest User

Untitled

a guest
Apr 11th, 2021
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _04._Mixed_up_Lists
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<int> firstList = Console.ReadLine()
  12.                              .Split(' ',StringSplitOptions.RemoveEmptyEntries)
  13.                              .Select(int.Parse)
  14.                              .ToList();
  15.  
  16.             List<int> secondList = Console.ReadLine()
  17.                              .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  18.                              .Select(int.Parse)
  19.                              .ToList();
  20.  
  21.             //намираме по-малкия лист
  22.  
  23.             int smallerListCount = Math.Min(firstList.Count, secondList.Count);
  24.  
  25.             //декларираме си нов лист, където ще слепим 2та инпут листа
  26.  
  27.             List<int> combinedList = new List<int>(smallerListCount);
  28.  
  29.             //обръщаме 2-рия лист, знаем, че ни трябват неговите елементи отзад-напред
  30.             //по този начин въртим само 1 цикъл
  31.  
  32.             secondList.Reverse();
  33.  
  34.             for (int i = 0; i < smallerListCount; i++)
  35.             {
  36.                 combinedList.Add(firstList[i]);
  37.                 combinedList.Add(secondList[i]);
  38.             }
  39.  
  40.             int constraintOne = 0;
  41.             int constraintTwo = 0;
  42.  
  43.             if (firstList.Count > secondList.Count)
  44.             {
  45.                 constraintOne = firstList[firstList.Count - 1];
  46.                 constraintTwo = firstList[firstList.Count - 2];
  47.             }
  48.             else
  49.             {
  50.                 constraintOne = secondList[secondList.Count - 1];
  51.                 constraintTwo = secondList[secondList.Count - 2];
  52.             }
  53.  
  54.             //намираме по-голямото/по-малкото на последните 2 елемента,които ще ползваме за констрейнт
  55.  
  56.             int smallerConstraintNum = Math.Min(constraintTwo, constraintOne);
  57.             int biggerConstraintNum = Math.Max(constraintTwo, constraintOne);
  58.  
  59.             List<int> listToPrint = new List<int>(20);
  60.  
  61.             //пълним си финалния масив само с елементи които влизат между 2-та констрейнта
  62.  
  63.             listToPrint = combinedList.Where(n => n > smallerConstraintNum && n < biggerConstraintNum).ToList();
  64.  
  65.             listToPrint.Sort();
  66.  
  67.             Console.WriteLine(string.Join(" ", listToPrint));
  68.         }
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement