SHARE
TWEET

Problem05SubsetSums

g-stoyanov Dec 11th, 2012 219 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. class Problem05SubsetSums
  4. {
  5.     static void Main()
  6.     {
  7.         short result = 0;
  8.         long temp = 0;
  9.         string combination = "";
  10.         long s = long.Parse(Console.ReadLine());
  11.         byte n = byte.Parse(Console.ReadLine());
  12.         long[] numberN = new long[n];
  13.         for (int i = 0; i < n; i++)
  14.         {
  15.             numberN[i] = long.Parse(Console.ReadLine());
  16.         }
  17.  
  18.         for (int i = 1; i < Math.Pow(2, n); i++)
  19.         {
  20.             combination = Convert.ToString(i, 2).PadLeft(n, '0');
  21.             temp = 0;
  22.             for (int a = 0; a < combination.Length; a++)
  23.             {
  24.                 if (combination[a] == '1')
  25.                 {
  26.                     temp = temp + numberN[a];
  27.                 }
  28.             }
  29.             if (temp == s)
  30.             {
  31.                 result++;
  32.             }
  33.         }
  34.         Console.WriteLine(result);
  35.     }
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top