TheBulgarianWolf

Merging Lists

Nov 10th, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SoftUniLists
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.Write("Enter the first list: ");
  12.             List<int> list1 = ReadList();
  13.             Console.Write("Enter the second list: ");
  14.             List<int> list2 = ReadList();
  15.             int resultListCount;
  16.             int remainingCount = 0;
  17.             if(list1.Count > list2.Count)
  18.             {
  19.                 resultListCount = list2.Count;
  20.                 remainingCount = list1.Count - list2.Count;
  21.             }
  22.             else
  23.             {
  24.                 resultListCount = list1.Count;
  25.                 remainingCount = list2.Count - list1.Count;
  26.             }
  27.  
  28.             List<int> resultList = new List<int>(list1.Count + list2.Count);
  29.             for(int i = 0; i < resultListCount; i++)
  30.             {
  31.                 resultList.Add(list1[i]);
  32.                 resultList.Add(list2[i]);
  33.             }
  34.  
  35.             if (list1.Count > list2.Count)
  36.             {
  37.                 for(int i = list1.Count - remainingCount; i < list1.Count; i++)
  38.                 {
  39.                     resultList.Add(list1[i]);
  40.                 }
  41.             }
  42.             else
  43.             {
  44.                 for (int i = list2.Count-remainingCount; i < list2.Count; i++)
  45.                 {
  46.                     resultList.Add(list2[i]);
  47.                 }
  48.             }
  49.  
  50.             PrintList(resultList);
  51.  
  52.  
  53.         }
  54.  
  55.         static List<int> ReadList(int n)
  56.         {
  57.             var list = new List<int>();
  58.             for (int i = 0; i < n; i++)
  59.             {
  60.                 list.Add(int.Parse(Console.ReadLine()));
  61.             }
  62.  
  63.             return list;
  64.         }
  65.  
  66.         static List<int> ReadList()
  67.         {
  68.             List<int> list = new List<int>();
  69.             var line = Console.ReadLine();
  70.             list = line.Split().Select(int.Parse).ToList();
  71.             return list;
  72.         }
  73.  
  74.         static void PrintList(List<int> list)
  75.         {
  76.             for (int i = 0; i < list.Count; i++)
  77.             {
  78.                 Console.WriteLine(list[i]);
  79.             }
  80.         }
  81.     }
  82. }
  83.  
Add Comment
Please, Sign In to add comment