Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication6
- {
- class Program
- {
- static void Main(string[] args)
- {
- int sum = int.Parse(Console.ReadLine());
- int[] input = Console.ReadLine().Split(' ').Select(int.Parse).Distinct().ToArray();
- List<List<int>> listToBeOrdered = new List<List<int>>();
- double combinations = Math.Pow(2, input.Length);
- bool thereAreElements = false;
- for (int i = 1; i < combinations; i++)
- {
- var subset = new List<int>();
- for (int bitPosition = 0; bitPosition < input.Length; bitPosition++)
- {
- if ((i & (1 << bitPosition)) != 0)
- {
- subset.Add(input[bitPosition]);
- }
- }
- if (subset.Sum() == sum)
- {
- listToBeOrdered.Add(subset);
- thereAreElements = true;
- }
- }
- if (!thereAreElements)
- {
- Console.WriteLine("No matching subsets.");
- }
- else
- {
- listToBeOrdered.ForEach(p => p.Sort());
- listToBeOrdered = listToBeOrdered.OrderBy(a => a.Count).ThenBy(b => b.First()).ToList();
- listToBeOrdered.ForEach(p => Console.WriteLine("{0} = {1}", string.Join(" + ", p), sum));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment