Advertisement
Guest User

6.SubsetSums

a guest
Sep 18th, 2015
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class FindSubsetSums
  8. {
  9.     static void Main()
  10.     {
  11.         int n = int.Parse(Console.ReadLine());
  12.         List<int> numbers = Console.ReadLine().Split(' ').Select(int.Parse).Distinct().ToList();
  13.  
  14.         List<List<int>> allSets = GenerateSubsets(numbers);
  15.         List<List<int>> goodSets = GetGoodSubsets(allSets, n);
  16.         PrintSubsets(goodSets, n);    
  17.     }
  18.  
  19.     static List<List<int>> GenerateSubsets(List<int> inputNumbers)
  20.     {
  21.         List<List<int>> allCombinations = new List<List<int>>();
  22.         List<int> subset = new List<int>();
  23.         int subsetsNumber = 1 << inputNumbers.Count;
  24.  
  25.         for (int i = 1; i < subsetsNumber; i++)
  26.         {
  27.             int pos = inputNumbers.Count - 1;
  28.             int bitmask = i;
  29.  
  30.             while (bitmask > 0)
  31.             {
  32.                 if ((bitmask & 1) == 1)
  33.                 {
  34.                     subset.Add(inputNumbers[pos]);
  35.                 }
  36.                 bitmask >>= 1;
  37.                 pos--;
  38.             }
  39.             allCombinations.Add(new List<int>(subset));
  40.             subset.Clear();
  41.         }
  42.         return allCombinations;
  43.     }
  44.  
  45.     static List<List<int>> GetGoodSubsets(List<List<int>> allCombinations, int number)
  46.     {
  47.         List<List<int>> properSets = new List<List<int>>();
  48.  
  49.         foreach (List<int> list in allCombinations)
  50.         {
  51.             if (list.Sum() == number)
  52.             {
  53.                 properSets.Add(list);
  54.             }
  55.         }
  56.         return properSets;
  57.     }
  58.  
  59.     static void PrintSubsets(List<List<int>> properSubsets, int number)
  60.     {
  61.         if (properSubsets.Count == 0)
  62.         {
  63.             Console.WriteLine("No matching subsets.");
  64.         }
  65.         else
  66.         {
  67.             foreach (List<int> subset in properSubsets)
  68.             {
  69.                 foreach (int num in subset)
  70.                 {
  71.                     if (subset.IndexOf(num) != subset.Count - 1)
  72.                     {
  73.                         Console.Write(num + " + ");
  74.                     }
  75.                     else
  76.                     {
  77.                         Console.Write(num);
  78.                     }
  79.                 }
  80.                 Console.WriteLine(" = {0}", number);
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement