Advertisement
v_staykov

SubsetSumIfZero

Dec 3rd, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.30 KB | None | 0 0
  1. using System;
  2.  
  3. class CheckSumOfSubsets
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("Please insert the five members of the set.\n");
  8.         string consoleInput;
  9.  
  10.         Console.Write("a: ");
  11.         consoleInput = Console.ReadLine();
  12.         int a = int.Parse(consoleInput);
  13.  
  14.         Console.Write("b: ");
  15.         consoleInput = Console.ReadLine();
  16.         int b = int.Parse(consoleInput);
  17.  
  18.         Console.Write("c: ");
  19.         consoleInput = Console.ReadLine();
  20.         int c = int.Parse(consoleInput);
  21.  
  22.         Console.Write("d: ");
  23.         consoleInput = Console.ReadLine();
  24.         int d = int.Parse(consoleInput);
  25.  
  26.         Console.Write("e: ");
  27.         consoleInput = Console.ReadLine();
  28.         int e = int.Parse(consoleInput);
  29.         Console.WriteLine();
  30.  
  31.         byte counter = 0;
  32.  
  33.         //Checking for the whole set
  34.         if (a + b + c + d + e == 0)
  35.         {
  36.             Console.WriteLine("The sum of the whole set is zero!");
  37.             counter++;
  38.         }
  39.  
  40.         //Checking every subset of two members
  41.         if (a + b == 0)
  42.         {
  43.             counter++;
  44.             Console.WriteLine("The sum of {0}(a) and {1}(b) is 0", a, b);
  45.         }
  46.         if (a + c == 0)
  47.         {
  48.             counter++;
  49.             Console.WriteLine("The sum of {0}(a) and {1}(c) is 0", a, c);
  50.         }
  51.         if (a + d == 0)
  52.         {
  53.             counter++;
  54.             Console.WriteLine("The sum of {0}(a) and {1}(d) is 0", a, d);
  55.         }
  56.         if (a + e == 0)
  57.         {
  58.             counter++;
  59.             Console.WriteLine("The sum of {0}(a) and {1}(e) is 0", a, e);
  60.         }
  61.         if (b + c == 0)
  62.         {
  63.             counter++;
  64.             Console.WriteLine("The sum of {0}(b) and {1}(c) is 0", b, c);
  65.         }
  66.         if (b + d == 0)
  67.         {
  68.             counter++;
  69.             Console.WriteLine("The sum of {0}(b) and {1}(d) is 0", b, d);
  70.         }
  71.         if (b + e == 0)
  72.         {
  73.             counter++;
  74.             counter++;
  75.             Console.WriteLine("The sum of {0}(b) and {1}(e) is 0", b, e);
  76.         }
  77.         if (c + d == 0)
  78.         {
  79.             counter++;
  80.             Console.WriteLine("The sum of {0}(c) and {1}(d) is 0", c, d);
  81.         }
  82.         if (c + e == 0)
  83.         {
  84.             counter++;
  85.             Console.WriteLine("The sum of {0}(c) and {1}(e) is 0", c, e);
  86.         }
  87.         if (d + e == 0)
  88.         {
  89.             counter++;
  90.             Console.WriteLine("The sum of {0}(d) and {1}(e) is 0", d, e);
  91.         }
  92.  
  93.         //Checking every subset of three members
  94.         if (a + b + c == 0)
  95.         {
  96.             counter++;
  97.             Console.WriteLine("The sum of {0}(a), {1}(b) and {2}(c) is 0", a, b, c);
  98.         }
  99.         if (a + b + d == 0)
  100.         {
  101.             counter++;
  102.             Console.WriteLine("The sum of {0}(a), {1}(b) and {2}(d) is 0", a, b, d);
  103.         }
  104.         if (a + b + e == 0)
  105.         {
  106.             counter++;
  107.             Console.WriteLine("The sum of {0}(a), {1}(b) and {2}(e) is 0", a, b, e);
  108.         }
  109.         if (a + c + d == 0)
  110.         {
  111.             counter++;
  112.             Console.WriteLine("The sum of {0}(a), {1}(c) and {2}(d) is 0", a, c, d);
  113.         }
  114.         if (a + c + e == 0)
  115.         {
  116.             counter++;
  117.             Console.WriteLine("The sum of {0}(a), {1}(c) and {2}(e) is 0", a, c, e);
  118.         }
  119.         if (a + d + e == 0)
  120.         {
  121.             counter++;
  122.             Console.WriteLine("The sum of {0}(a), {1}(d) and {2}(e) is 0", a, d, e);
  123.         }
  124.         if (b + c + d == 0)
  125.         {
  126.             counter++;
  127.             Console.WriteLine("The sum of {0}(b), {1}(c) and {2}(d) is 0", b, c, d);
  128.         }
  129.         if (b + c + e == 0)
  130.         {
  131.             counter++;
  132.             Console.WriteLine("The sum of {0}(b), {1}(c) and {2}(e) is 0", b, c, e);
  133.         }
  134.         if (b + d + e == 0)
  135.         {
  136.             counter++;
  137.             Console.WriteLine("The sum of {0}(b), {1}(d) and {2}(e) is 0", b, d, e);
  138.         }
  139.         if (c + d + e == 0)
  140.         {
  141.             counter++;
  142.             Console.WriteLine("The sum of {0}(c), {1}(d) and {2}(e) is 0", c, d, e);
  143.         }
  144.  
  145.         //Checking for everysubset of four members
  146.         if (a + b + c + d == 0)
  147.         {
  148.             counter++;
  149.             Console.WriteLine("The sum of {0}(a), {1}(b), {2}(c) and {3}(d) is 0", a, b, c, d);
  150.         }
  151.         if (a + b + c + e == 0)
  152.         {
  153.             counter++;
  154.             Console.WriteLine("The sum of {0}(a), {1}(b), {2}(c) and {3}(e) is 0", a, b, c, e);
  155.         }
  156.         if (a + b + d + e == 0)
  157.         {
  158.             counter++;
  159.             Console.WriteLine("The sum of {0}(a), {1}(b), {2}(d) and {3}(e) is 0", a, b, d, e);
  160.         }
  161.         if (a + c + d + e == 0)
  162.         {
  163.             counter++;
  164.             Console.WriteLine("The sum of {0}(a), {1}(c), {2}(d) and {3}(e) is 0", a, c, d, e);
  165.         }
  166.         if (b + c + d + e == 0)
  167.         {
  168.             counter++;
  169.             Console.WriteLine("The sum of {0}(b), {1}(c), {2}(d) and {3}(e) is 0", b, c, d, e);
  170.         }
  171.         if (counter == 1)
  172.         {
  173.             Console.WriteLine("\nThere is 1 subset with sum equal to zero!");
  174.         }
  175.         else
  176.         {
  177.             Console.WriteLine("\nThere are {0} subsets with sum equal to zero!", counter);
  178.         }
  179.     }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement