Advertisement
g-stoyanov

Problem05SubsetSums

Dec 11th, 2012
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement