Advertisement
cap7ainjack

Homework_1_7

Sep 19th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 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. namespace Hmwrk_1_7
  8. {
  9. internal class Program
  10. {
  11. private static void Main(string[] args)
  12. {
  13. int sum = int.Parse(Console.ReadLine());
  14.  
  15. int[] input = Console.ReadLine().Split(' ').Select(int.Parse).Distinct().ToArray();
  16.  
  17. List<int> currentComb = new List<int>();
  18. List<List<int>> allcombs = new List<List<int>>();
  19.  
  20. double combinations = Math.Pow(2, input.Length);
  21.  
  22. for (int mask = 0; mask < combinations; mask++)
  23. {
  24. for (int i = 0; i < input.Length; i++)
  25. {
  26. if ((mask & (1 << i)) != 0)
  27. {
  28. currentComb.Add(input[i]);
  29. }
  30. }
  31.  
  32. if (currentComb.Sum() == sum)
  33. {
  34. allcombs.Add(currentComb);
  35. }
  36.  
  37. currentComb.Clear();
  38.  
  39. }
  40.  
  41.  
  42. if (allcombs.Count == 0)
  43. {
  44. Console.WriteLine("No !");
  45. }
  46.  
  47. else
  48. {
  49.  
  50. allcombs.ForEach(list => list.Sort());
  51. allcombs = allcombs.OrderBy(a => a.Count).ThenBy(b => b.First()).ToList();
  52.  
  53. allcombs.ForEach(list => Console.WriteLine("{0} = {1}", string.Join(" + ", list), sum));
  54.  
  55. }
  56.  
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement