# SoftUni Zero subset

a guest
Oct 18th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. using System;
2. using System.Collections.Generic;
3.
4. class ZeroSubset
5. {
6.     static void Main()
7.     {
8.         /*
9.          *We are given 5 integer numbers. Write a program that finds all subsets of these numbers whose sum is 0 .
10.          *Assume that repeating the same subset several times is not a problem
11.         */
12.
13.         Console.WriteLine("Please write in whole numbers separated by one single white space.");
14.         Console.WriteLine("If you write less or some nonsense you'll also receive some nonsense too :)");
15.
16.         string[] inputResult = Console.ReadLine().Split(' ');
17.
18.         //step 1: add all of these in an array
19.
20.         int[] inputNumbers = new int[5];
21.
22.         for (int i = 0; i < Math.Min(5,inputResult.Length); i++)
23.             {
24.                 int.TryParse(inputResult[i], out inputNumbers[i]);
25.             }
26.
27.
28.         //step 2: calculate all combinations of 0,1,2,3,4 with 2, 3 and 4 number chosen
29.         // https://www.mathsisfun.com/combinatorics/combinations-permutations-calculator.html
30.         // define them in array as below
31.
32.         int[][] jaggedIndices = new int[][]
33.         {
34.             new int[]{0,1}, new int[]{0,2}, new int[]{0,3}, new int[]{0,4},
35.             new int[]{1,2}, new int[]{1,3}, new int[]{1,4},
36.             new int[]{2,3}, new int[]{2,4}, new int[]{3,4},
37.             new int[]{0,1,2}, new int[]{0,1,3}, new int[]{0,1,4},
38.             new int[]{0,2,3}, new int[]{0,2,4}, new int[]{0,3,4},
39.             new int[]{1,2,3}, new int[]{1,2,4}, new int[]{1,3,4},
40.             new int[]{2,3,4},
41.             new int[]{0,1,2,3}, new int[]{0,1,2,4}, new int[]{0,1,3,4},
42.             new int[]{0,2,3,4}, new int[]{1,2,3,4},
43.             new int[]{0,1,2,3,4}
44.         };
45.
46.
47.         bool flagAtLeastOne = false;
48.         for (int i = 0; i < jaggedIndices.Length; i++)
49.         {
50.             int tempsum = 0;
51.             string tempstring="";
52.             for (int j = 0; j < jaggedIndices[i].Length; j++)
53.             {
54.
55.                 tempsum += inputNumbers[jaggedIndices[i][j]];
56.                 tempstring += inputNumbers[jaggedIndices[i][j]].ToString() + (j == jaggedIndices[i].Length-1 ? " = 0" : " + ");
57.
58. //                Console.WriteLine(tempsum);
59.
60.             }
61.             if (tempsum==0)
62.             {
63.                 Console.WriteLine(tempstring);
64.                 flagAtLeastOne = true;
65.             }
66.         }
67.
68.         if(!flagAtLeastOne)
69.         {
70.             Console.WriteLine("no zero subset");
71.         }
72.
73.         //end of program - can be tested with F5 as well (if your antivirus is stopping you)
74.         Console.WriteLine(Environment.NewLine + "Please press any key to continue.");
76.     }
77.
78.
79. }