Advertisement
Guest User

Pr07SortedSubsetSums

a guest
Sep 20th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. // Modify the program you wrote for the previous problem to print the results in the following way:
  6. // each line should contain the operands (numbers that form the desired sum) in ascending order;
  7. // the lines containing fewer operands should be printed before those with more operands;
  8. // when two lines have the same number of operands, the one containing the smallest operand should be printed first.
  9. // If two or more lines contain the same number of operands and have the same smallest operand,
  10. // the order of printing is not important.
  11.  
  12. namespace Pr07SortedSubsetSums
  13. {
  14.     class Pr07SortedSubsetSums
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             int sum = int.Parse(Console.ReadLine());
  19.             int[] nums = Console.ReadLine().Split(' ').Select(n => int.Parse(n)).Distinct().ToArray();
  20.             int numsLength = nums.Length;
  21.             List<int> subset = new List<int>();
  22.             List<string> subsetStrings = new List<string>();
  23.             bool isMatch = false;
  24.             int combos = 1 << numsLength;
  25.             for (int mask = 0; mask < combos; mask++)
  26.             {
  27.                 int subsetSum = int.MinValue + 2;
  28.                 for (int i = 0; i < numsLength; i++)
  29.                 {
  30.                     if ((mask & (1 << i)) != 0)
  31.                     {
  32.                         if (subsetSum == int.MinValue + 2)
  33.                         {
  34.                             subsetSum = 0;
  35.                         }
  36.                         subset.Add(nums[i]);
  37.                         subsetSum += nums[i];
  38.                     }
  39.                 }
  40.                 if (subsetSum == sum)
  41.                 {
  42.                     subsetStrings.Add(string.Join(" + ", subset) + " = " + sum);
  43.                     isMatch = true;
  44.                 }
  45.                 subset.Clear();
  46.             }
  47.             if (isMatch == false)
  48.             {
  49.                 Console.WriteLine("No matching subsets.");
  50.             }
  51.             else
  52.             {
  53.                 foreach (string subsetString in SortByLength(subsetStrings))
  54.                 {
  55.                     Console.WriteLine(subsetString);
  56.                 }
  57.             }
  58.         }
  59.  
  60.         static IEnumerable<string> SortByLength(List<string> subsetStrings)
  61.         {
  62.             var sorted = from s in subsetStrings orderby s.Length ascending select s;
  63.             return sorted;
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement