SHARE
TWEET

ZeroSubset

g-stoyanov Apr 8th, 2014 340 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Text;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         int[] numbers = Console.ReadLine().Split(' ').Select(n => int.Parse(n)).ToArray();
  10.         bool subsetFounded = false;
  11.         for (int i = (1 << numbers.Length) - 1; i > 0; i--)
  12.         {
  13.             int sum = 0;
  14.             StringBuilder sb = new StringBuilder();
  15.             for (int a = 0; a < numbers.Length; a++)
  16.             {
  17.                 int maskNum = i >> a;
  18.                 if (maskNum % 2 != 0)
  19.                 {
  20.                     sum += numbers[a];
  21.                     sb.Append(string.Format("{0} + ", numbers[a]));
  22.                 }
  23.             }
  24.  
  25.             if (sum == 0 && sb.Length != 0)
  26.             {
  27.                 subsetFounded = true;
  28.                 Console.WriteLine("{0} = 0", sb.Remove(sb.Length - 2, 2));
  29.             }
  30.         }
  31.  
  32.         if (!subsetFounded)
  33.         {
  34.             Console.WriteLine("no zero subset");
  35.         }
  36.     }
  37. }
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