Advertisement
APXOHT

Untitled

Jan 6th, 2013
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System;
  2.  
  3. class SubsetSum
  4. {
  5.     static void Main()
  6.     {
  7.         int[] arrayOfNumbers = {2, 1, 2, 4, 3, 5, 2, 5, 2, 4, 3, 7, 2, 9, 11, 2, 4, 3, 5, 2, 6};
  8.         int maxi = (int)Math.Pow(2, arrayOfNumbers.Length) - 1;
  9.         int S = int.Parse(Console.ReadLine());
  10.         bool hasSum = false;
  11.         for (int i = 1; i <= maxi; i++)
  12.         {
  13.             int currentSum = 0;
  14.             for (int j = 1; j <= arrayOfNumbers.Length; j++)
  15.             {
  16.                 if (((i >> (j - 1)) & 1) == 1)
  17.                 {
  18.                     currentSum += arrayOfNumbers[j - 1];
  19.                 }
  20.             }
  21.             if (currentSum == S)
  22.             {
  23.                 hasSum = true;
  24.             }
  25.         }
  26.         if (hasSum)
  27.         {
  28.             Console.WriteLine("Yes.");
  29.         }
  30.         else
  31.         {
  32.             Console.WriteLine("No.");
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement