ellapt

T7.18.LongestSortedSubset

Jan 15th, 2013
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class LongestSortedSubset
  5. {
  6. static void Main()
  7. {
  8. string inputVar;
  9. int n;
  10. Console.WriteLine("Find the longest sorted subset of an array of long integers");
  11. do
  12. {
  13. Console.Write("Enter array length: ");
  14. }
  15. while (!(int.TryParse(inputVar = Console.ReadLine(), out n)) || n == 0);
  16.  
  17. int[] arrayOfints = new int[n];
  18.  
  19. Console.WriteLine("Enter the elements of the array:");
  20. for (int i = 0; i < n; i++)
  21. {
  22. arrayOfints[i] = int.Parse(Console.ReadLine());
  23. }
  24.  
  25. int subSetsCnt = (2 << arrayOfints.Length - 1) - 1;
  26. List<int> sortedResult = new List<int>();
  27. List<int> workList = new List<int>();
  28. int minValue;
  29. int counter = 0;
  30. int cnt = 0;
  31.  
  32. for (int i = 0; i <= subSetsCnt; i++)
  33. {
  34. counter = 0;
  35. minValue = int.MinValue;
  36. for (int k = 0; k < arrayOfints.Length; k++)
  37. {
  38. if ((((i >> k) & 1) == 1) && (minValue <= arrayOfints[k]))
  39. {
  40. minValue = arrayOfints[k];
  41. workList.Add(arrayOfints[k]);
  42. counter++;
  43. }
  44. }
  45.  
  46. if (counter > cnt)
  47. {
  48. sortedResult.Clear();
  49. cnt = counter;
  50. for (int j = 0; j < workList.Count; j++)
  51. {
  52. sortedResult.Add(workList[j]);
  53. }
  54. }
  55. workList.Clear();
  56. }
  57.  
  58. for (int i = 0; i < sortedResult.Count; i++)
  59. {
  60. Console.Write(sortedResult[i] + " ");
  61. }
  62. Console.WriteLine();
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment