Advertisement
Teodor92

Arrays.18.ElementsRemove

Jan 21st, 2013
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class SubSetSum
  5. {
  6.     static bool AreSorted(List<long> array)
  7.     {
  8.         bool isSorted = true;
  9.         for (int i = 0; i < array.Count - 1; i++)
  10.         {
  11.             if (!(array[i] <= array[i+1]))
  12.             {
  13.                 isSorted = false;
  14.             }
  15.         }
  16.         return isSorted;
  17.     }
  18.     static void Main()
  19.     {
  20.         //Console.WriteLine("Enter the number of elements:");
  21.         //long numberOfElements = long.Parse(Console.ReadLine());
  22.         //long[] elements = new long[numberOfElements];
  23.         long[] elements = { 6, 1, 4, 3, 0, 3, 6, 4, 5 };
  24.         //for (int i = 0; i < elements.Length; i++)
  25.         //{
  26.         //    Console.WriteLine("Enter element № {0}", i + 1);
  27.         //    elements[i] = long.Parse(Console.ReadLine());
  28.         //}
  29.         int len = 0;
  30.         int bestLen = 0;
  31.         List<long> bestResult = new List<long>();
  32.         int maxSubsets = (int)Math.Pow(2, elements.Length);
  33.         for (int i = 1; i < maxSubsets; i++)
  34.         {
  35.             List<long> result = new List<long>();
  36.  
  37.             for (int j = 0; j <= elements.Length; j++)
  38.             {
  39.                 int mask = 1 << j;
  40.                 int nAndMask = i & mask;
  41.                 int bit = nAndMask >> j;
  42.                 if (bit == 1)
  43.                 {
  44.                     result.Add(elements[j]);
  45.                     len++;
  46.                 }
  47.             }
  48.             if (AreSorted(result))
  49.             {
  50.                 if (len > bestLen)
  51.                 {
  52.                     bestLen = len;
  53.                     bestResult = result;
  54.                 }
  55.             }
  56.             len = 0;
  57.             //if (checkingSum == wantedSum)
  58.             //{
  59.             //    Console.WriteLine("Number of subest that have the sum of {0}", wantedSum);
  60.             //    counter++;
  61.             //    Console.WriteLine("This subset has a sum of {0} : {1} ", wantedSum, subset);
  62.             //}
  63.         }
  64.         foreach (var item in bestResult)
  65.         {
  66.             Console.Write("{0} ",item);
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement