AleksandarKostadinov

Ch.5.Ex.9.SumsOfSubsetsInNLongArray

Dec 21st, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Ex._09.SubsetsWihtSumZero
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine("This program is for counting how many subsets of a given set of \r\nnumbers have sum equal to zero.");
  14.             Console.Write("Enter how long the set must be. N = ");
  15.  
  16.             int n = int.Parse(Console.ReadLine());
  17.             int[] myArray = new int[n];
  18.             for (int currentEl = 0; currentEl < myArray.Length; currentEl++)
  19.             {
  20.                 myArray[currentEl] = int.Parse(Console.ReadLine());
  21.             }
  22.             // We have to sum every possible combination numbers in the array (every subset).
  23.             // So we sum or we don't sum any given number.Two choices.
  24.             // That makes 2 to the power of the numbers in the array possibilities.
  25.  
  26.             double possibilities = Math.Pow(2, n);
  27.             int zeroSums = 0;
  28.  
  29.             for (int currentPoss = 1; currentPoss < possibilities; currentPoss++)
  30.             {
  31.                 int[] arrayForCalc = new int[myArray.Length];
  32.                 for (int p = 0; p < myArray.Length; p++)
  33.                 {
  34.                     arrayForCalc[p] = myArray[p];
  35.                 }
  36.  
  37.                 // Whether we sum or not sum is (true or false) a number can
  38.                 // be represented whit 1's and 0's.
  39.                 // Every possibilitie of 1's and 0's is the bitwise representation
  40.                 // of the var "currentPoss" so we place it in an array.
  41.  
  42.                 int[] bitwiseI = new int[myArray.Length];
  43.                 for (int j = 0; j < myArray.Length; j++)
  44.                 {
  45.                     bitwiseI[j] = 1 & (currentPoss >> j);
  46.                 }
  47.                 // We multiply the two arrays so we can get only the numbers we want to sum.
  48.                 for (int h = 0; h < myArray.Length; h++)
  49.                 {
  50.                     arrayForCalc[h] *= bitwiseI[h];
  51.                 }
  52.                 int sum = 0;
  53.                 for (int k = 0; k < myArray.Length; k++)
  54.                 {
  55.                     sum += arrayForCalc[k];
  56.                 }
  57.                 if (sum == 0)
  58.                 {
  59.                     zeroSums++;
  60.                 }
  61.  
  62.             }
  63.             Console.WriteLine("The number of subsets that have sum zero is {0}." , zeroSums);
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment