コード例 #1
0
    static void Main()
    {
        // Input array
        Console.WriteLine("Enter an array of integer elements on a single line, separated by commas:");
        string input = Console.ReadLine();

        // Split the elements into an array
        string[] inputElements = input.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
        // Fill an integer array from string array
        int[] array = new int[inputElements.Length];
        for (int i = 0; i < inputElements.Length; i++)
        {
            array[i] = int.Parse(inputElements[i]);
        }
        int n = array.Length;

        Console.Write("Enter the sum S = ");
        int s = int.Parse(Console.ReadLine());

        Console.Write("Enter Subset size K = ");
        int k = int.Parse(Console.ReadLine());

        int PowerSetSize = 1 << n;                                             // equiavalent to Math.Pow(2, n)

        var PowerSet =                                                         // each subset contains a set of array elements
                       from PowerSetIndex in Enumerable.Range(0, PowerSetSize) // for each new PowerSet Subset
                       select                                                  // Return Elements
                       from ArrayIndex in Enumerable.Range(0, array.Count())   // From the input Array toSearch
                       where (PowerSetIndex & (1 << ArrayIndex)) != 0          // If PowerSet Index and ArrayIndex
                       select array[ArrayIndex];                               // have a matching bit return that Element

        // Find Subsets with Matching Sum
        PowerSet =
            from SubSet in PowerSet                         // for each existing subset in PowerSet
            where SubSet.Sum() == s && SubSet.Count() == k
            select SubSet;                                  // Return the matching subsets

        // output
        if (PowerSet.Count() > 0)       //if PowerSet has at least 1 matching subset
        {
            Console.WriteLine("yes");
        }
        else
        {
            Console.WriteLine("no");
        }

        // For each subset, separated by NewLine join it's sub elements in a string
        //Console.Write(string.Join("\n", PowerSet.Select(subset => string.Join(" ", subset))));
        //Console.WriteLine();
    }
コード例 #2
0
ファイル: SubsetKwithSumS.cs プロジェクト: slapshott/Homework
        static void Main()
        {
            // input
            int arraySize = int.Parse(Console.ReadLine());

            // get array
            int[] toSearch = new int[arraySize];

            for (int i = 0; i < arraySize; i++)
            {
                toSearch[i] = int.Parse(Console.ReadLine());
            }

            // get SUM
            int toFindSum = int.Parse(Console.ReadLine());

            // get Subset size
            int toFindSubsetLen = int.Parse(Console.ReadLine());

            int PowerSetSize = 1 << arraySize;                                      // equiavalent to Math.Pow(2, arraySize)

            var PowerSet =                                                          // each subset contains a set of array elements
                           from PowerSetIndex in Enumerable.Range(0, PowerSetSize)  // for each new PowerSet Subset
                           select                                                   // Return Elements
                           from ArrayIndex in Enumerable.Range(0, toSearch.Count()) // From the input Array toSearch
                           where (PowerSetIndex & (1 << ArrayIndex)) != 0           // If PowerSet Index and ArrayIndex
                           select toSearch[ArrayIndex];                             // have a matching bit return that Element

            // Find Subsets with Matching Sum
            PowerSet =
                from SubSet in PowerSet                     // for each existing subset in PowerSet

                where SubSet.Sum() == toFindSum &&          // return if sum == toFindSUM
                SubSet.Count() == toFindSubsetLen           // AND Count == required Length

                select SubSet;                              // Return the matching subsets

            // if PowerSet has at least 1 matching subset
            if (PowerSet.Count() > 0)
            {
                Console.WriteLine("yes");   // Print YES
            }
            else
            {
                Console.WriteLine("no");    // If not Print NO
            }

            // TODO: DELETE
            Console.Write(string.Join("\n", PowerSet.Select(                     // For each subset, separated by NewLine
                                          subset => string.Join(" ", subset)))); // join it's sub elements in a string
        }