Advertisement
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 Hmwrk_1_7
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- int sum = int.Parse(Console.ReadLine());
- int[] input = Console.ReadLine().Split(' ').Select(int.Parse).Distinct().ToArray();
- // List<int> currentComb = new List<int>();
- List<List<int>> allcombs = new List<List<int>>();
- double combinations = Math.Pow(2, input.Length);
- for (int mask = 0; mask < combinations; mask++)
- {
- List<int> currentComb = new List<int>(); // Added
- for (int i = 0; i < input.Length; i++)
- {
- if ((mask & (1 << i)) != 0)
- {
- currentComb.Add(input[i]);
- }
- }
- if (currentComb.Sum() == sum)
- {
- currentComb.Sort(); // Added bonus
- allcombs.Add(currentComb);
- }
- // currentComb.Clear();
- }
- if (allcombs.Count == 0)
- {
- Console.WriteLine("No !");
- }
- else
- {
- // allcombs.ForEach(list => list.Sort());
- allcombs = allcombs.OrderBy(a => a.Count).ThenBy(b => b.First()).ToList();
- allcombs.ForEach(list => Console.WriteLine("{0} = {1}", string.Join(" + ", list), sum));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement