archangelmihail

SubsetSumLists

Dec 5th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 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 SubsetSumLists
  8. {
  9.     class SubsetSumLists
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.            
  14.             checked
  15.             {
  16.                
  17.                 long s = long.Parse(Console.ReadLine());
  18.                 int n = int.Parse(Console.ReadLine());
  19.                 long[] numbers = new long[n];
  20.                 // initialization
  21.                 for (int i = 0; i < n; i++)
  22.                 {
  23.                     numbers[i] = long.Parse(Console.ReadLine());
  24.                 }
  25.  
  26.                 List<long> sums = new List<long>();
  27.                
  28.                 for (int i = 0; i < n; i++)
  29.                 {
  30.                     for (int j = 0, len = sums.Count; j < len; j++)
  31.                     {
  32.                         sums.Add(sums[j] + numbers[i]);
  33.                     }
  34.                     sums.Add(numbers[i]);
  35.                 }
  36.                 int count = 0;
  37.                 for (int i = 0; i < sums.Count; i++)
  38.                 {
  39.                     if (sums[i] == s)
  40.                     {
  41.                         count++;
  42.                     }
  43.                 }
  44.                 Console.WriteLine(count);
  45.  
  46.                 //long combinations = (long)Math.Pow(2, n) - 1;  // how  many combinations we have
  47.                 //long sumCounter = 0;
  48.  
  49.                 ////solution
  50.                 //for (int i = 1; i <= combinations; i++)
  51.                 //{
  52.  
  53.                 //    long tempSum = 0;
  54.  
  55.                 //    for (int j = 0; j < n; j++)
  56.                 //    {
  57.                 //        int mask = 1 << j;
  58.                 //        int iAndMask = mask & i;
  59.                 //        int bit = iAndMask >> j;
  60.                 //        /*  bit = (i >> j & 1); */
  61.                 //        if (bit == 1)
  62.                 //        {
  63.                 //            tempSum += numbers[j];
  64.                 //        }
  65.                 //    }
  66.                 //    if (tempSum == s)
  67.                 //    {
  68.                 //        sumCounter++;
  69.                 //    }
  70.                 //}
  71.                 //Console.WriteLine(sumCounter);
  72.             }
  73.            
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment