Advertisement
Guest User

Untitled

a guest
Dec 7th, 2014
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class ZeroSubset
  6. {
  7.      public static int sumToFind = 0;
  8.      public static Stack<int> stack = new Stack<int>();
  9.      public static int tempSum = 0;
  10.      public static int[] inputNumbers = { 1, 3, -4, -2, -1 };
  11.  
  12.      public static void RecursiveCalc(int[] data, int startIndex, int endIndex)
  13.      {
  14.           for (int currentIndex = startIndex; currentIndex < endIndex; currentIndex++)
  15.           {
  16.                 stack.Push(data[currentIndex]);
  17.                 tempSum += data[currentIndex];
  18.                 RecursiveCalc(data, currentIndex + 1, endIndex);
  19.                 tempSum -= (int) stack.Pop();
  20.           }
  21.           if (tempSum == sumToFind)
  22.           {
  23.                 printSet(stack);
  24.           }
  25.     }//end of RecursiveCalc()
  26.  
  27.     public static void printSet(Stack<int> stack)
  28.     {
  29.          StringBuilder sb = new StringBuilder();
  30.          sb.Append(sumToFind).Append(" = ");
  31.          foreach (int value in stack)
  32.          {
  33.               sb.Append(value).Append("+");
  34.          }
  35.          sb.Length--;
  36.          if (sb.Length < 4) return;
  37.          Console.WriteLine(sb.ToString());
  38.      }// end of printSet()
  39.  
  40.      static void Main()
  41.      {
  42.          RecursiveCalc(inputNumbers, 0, inputNumbers.Length);
  43.      }//end of Main()
  44. }// end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement