Advertisement
mivak

Problem10JoinLists

Mar 31st, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. namespace Task10JoinLists
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. class Task10JoinLists
  6. {
  7. static void Main()
  8. {
  9. /*Write a program that takes as input two lists of integers and joins them.
  10. * The result should hold all numbers from the first list, and all numbers
  11. * from the second list, without repeating numbers, and arranged in increasing
  12. * order. The input and output lists are given as integers, separated by a space,
  13. * each list at a separate line.*/
  14.  
  15. Console.WriteLine("Please enter two lists of integers, separated by a space, each list at a separate line");
  16. string firstText = Console.ReadLine();
  17. string secondText = Console.ReadLine();
  18.  
  19. string[] firstNumbers = firstText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  20. string[] secondNumbers = secondText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  21. List<int> num = new List<int>();
  22.  
  23. for (int i = 0; i < firstNumbers.Length; i++)
  24. {
  25. num.Add(int.Parse(firstNumbers[i]));
  26. }
  27.  
  28. for (int i = 0; i < secondNumbers.Length; i++)
  29. {
  30. num.Add(int.Parse(secondNumbers[i]));
  31. }
  32.  
  33. num.Sort();
  34.  
  35. HashSet<int> numbers = new HashSet<int>();//Hashset е почти като листа, но приема само уникални стойности,
  36. //т.е. като някоя стойност се повтаря тя не влиза в hashset-a
  37. for (int i = 0; i < num.Count; i++)
  38. {
  39. numbers.Add(num[i]);
  40. }
  41.  
  42. foreach (var number in numbers)
  43. {
  44. Console.Write(number + " ");
  45. }
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement