Advertisement
Guest User

Untitled

a guest
Sep 18th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. class SubsetSums
  6. {
  7.  
  8. static void Main()
  9. {
  10. int sum = int.Parse(Console.ReadLine());
  11. int[] input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  12. bool isMatch = false;
  13. List<int> subset = new List<int>();
  14.  
  15. int combinations = (int)Math.Pow(2, input.Length);
  16. for(int mask =0; mask<combinations;mask++)
  17. {
  18. for (int j=0;j<input.Length;j++)
  19. {
  20. if ((mask & (1<<j)) != 0)
  21. {
  22. subset.Add(input[j]);
  23. }
  24. }
  25. if (subset.Sum() == sum)
  26. {
  27. Console.WriteLine("{0} = {1}", string.Join(" + ", subset), sum);
  28. isMatch = true;
  29. }
  30. subset.Clear();
  31. }
  32. if(isMatch==false)
  33. {
  34. Console.WriteLine("No matching subsets.");
  35. }
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement