Advertisement
Stephen_MS

Zero Subset

Nov 24th, 2015
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. using System;
  2.  
  3. class ZeroSubset
  4. {
  5.     static void Main()
  6.     {
  7.         /* We are given 5 integer numbers. Write a program that finds all subsets of these numbers whose sum is 0.
  8.          * Assume that repeating the same subset several times is not a problem     */
  9.         int[] arrayInt = new int[5];
  10.         Console.WriteLine("Enter a five integers");
  11.         for (int counter = 0; counter < arrayInt.Length; counter++)    // input 5 integers inta array
  12.         {
  13.             Console.Write("int [{0}]: ", counter + 1);
  14.             arrayInt[counter] = Convert.ToInt32(Console.ReadLine());
  15.         }
  16.         int counterOfZeroSums = 0;
  17.  
  18.         // check for sum of two numbers -> using 2 loops: i, j
  19.         for (int i = 0; i < arrayInt.Length - 1; i++)
  20.         {
  21.             for (int j = (i + 1); j < arrayInt.Length; j++)
  22.             {
  23.                 if (arrayInt[i] + arrayInt[j] == 0)
  24.                 {
  25.                     Console.WriteLine("{0} + {1} = 0", arrayInt[i], arrayInt[j]);
  26.                     counterOfZeroSums++;
  27.                 }
  28.  
  29.             }
  30.         }
  31.  
  32.         // check for sum of three numbers -> using 3 loops: i, j, k
  33.         for (int i = 0; i < arrayInt.Length - 2; i++)
  34.         {
  35.             for (int j = (i + 1); j < arrayInt.Length - 1; j++)
  36.             {
  37.                 for (int k = (j + 1); k < arrayInt.Length; k++)
  38.                 {
  39.                     if (arrayInt[i] + arrayInt[j] + arrayInt[k] == 0)
  40.                     {
  41.                         Console.WriteLine("{0} + {1} + {2} = 0", arrayInt[i], arrayInt[j], arrayInt[k]);
  42.                         counterOfZeroSums ++;
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.  
  48.         // check for sum of four numbers -> using 4 loops: i, j, k, l
  49.         for (int i = 0; i < arrayInt.Length - 3; i++)
  50.         {
  51.             for (int j = (i + 1); j < arrayInt.Length - 2; j++)
  52.             {
  53.                 for (int k = (j + 1); k < arrayInt.Length - 1; k++)
  54.                 {
  55.                     for (int l = (k + 1); l < arrayInt.Length; l++)
  56.                     {
  57.                         if (arrayInt[i] + arrayInt[j] + arrayInt[k] + arrayInt[l] == 0)
  58.                         {
  59.                             Console.WriteLine("{0} + {1} + {2} + {3} = 0",
  60.                                 arrayInt[i], arrayInt[j], arrayInt[k], arrayInt[l]);
  61.                             counterOfZeroSums ++;
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.  
  68.  
  69.         // check for sum of all five numbers
  70.         if (arrayInt[0] + arrayInt[1] + arrayInt[2] + arrayInt[3] + arrayInt[4] == 0)
  71.         {
  72.             Console.WriteLine("{0} + {1} + {2} + {3} + {4} = 0",
  73.                 arrayInt[0], arrayInt[1], arrayInt[2], arrayInt[3], arrayInt[4]);
  74.             counterOfZeroSums ++;
  75.         }
  76.        
  77.         if (counterOfZeroSums == 0)
  78.         {
  79.             Console.WriteLine("no zero subset");
  80.         }
  81.         else
  82.         {
  83.             Console.WriteLine("\nTotal: {0} zero subsets", counterOfZeroSums);
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement