Advertisement
stak441

Untitled

Dec 7th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1.  static void Main(string[] args)
  2.         {
  3.             int a = -14;                         // Works properly, but sometimes repetition occurs (when there are equal variables)
  4.             int b = 6;
  5.             int c = -2;
  6.             int d = -4;
  7.             int e = 6;
  8.             int[] array = new int[]{ a, b, c, d, e };
  9.             bool noSet = true;            
  10.  
  11.  
  12.             if (a + b + c + d + e == 0)
  13.             {
  14.                 Console.WriteLine("The whole selection sums to 0");
  15.                 noSet = false;
  16.             }
  17.             for (int i = 0; i < 5; i++)
  18.             {
  19.                 int tempsum = array[i];
  20.                 for (int j = 0; j < 5 && j!= i; j++)
  21.                 {
  22.                     int tempsum1 = tempsum + array[j];
  23.                     if (tempsum1 == 0)
  24.                     {
  25.                         Console.WriteLine("The subsets that sum to 0 are: ({0}, {1})", array[i], array[j]);
  26.                         noSet = false;
  27.                     }
  28.                     for (int k = 0; k < 5 && (k!=j) && (k!=i); k++)
  29.                     {
  30.                         int tempsum2 = tempsum1 + array[k];
  31.                         if (tempsum2 == 0)
  32.                         {
  33.                             Console.WriteLine("The subsets that sum to 0 are: ({0}, {1}, {2})", array[i], array[j], array[k]);
  34.                             noSet = false;
  35.                         }
  36.                         for (int l = 0; l < 5 && (l!=k) && (l!=j) && (l!=i); l++)
  37.                         {
  38.                             int tempsum3 = tempsum2 + array[l];
  39.                             if (tempsum3 == 0)
  40.                             {
  41.                                 Console.WriteLine("The subsets that sum to 0 are: ({0}, {1}, {2}, {3})", array[i],
  42.                                     array[j], array[k], array[l]);
  43.                                 noSet = false;
  44.                             }
  45.                            
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.             if (noSet)
  51.             {
  52.                 Console.WriteLine("No subsets sum to 0");
  53.             }
  54.            
  55.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement