ntodorova

09_SumOfSubsetIs0

Nov 2nd, 2012
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System;
  2.  
  3. class SumOfSubsetIs0
  4. {
  5.     static void Main()
  6.     {
  7.         int a;
  8.         int b;
  9.         int c;
  10.         int d;
  11.         int e;
  12.         bool flagSum = false;
  13.  
  14.         Console.Write("Please enter the first number: ");
  15.         string firstStr = Console.ReadLine();
  16.  
  17.         Console.Write("Please enter the second number: ");
  18.         string secondStr = Console.ReadLine();
  19.  
  20.         Console.Write("Please enter the third number: ");
  21.         string thirdStr = Console.ReadLine();
  22.  
  23.         Console.Write("Please enter the fourth number: ");
  24.         string fourthStr = Console.ReadLine();
  25.  
  26.         Console.Write("Please enter the fifth number: ");
  27.         string fifthStr = Console.ReadLine();
  28.  
  29.         if (!int.TryParse(firstStr, out a))
  30.         {
  31.             Console.WriteLine("Invalid number: {0}", firstStr);
  32.         }
  33.         else if (!int.TryParse(secondStr, out b))
  34.         {
  35.             Console.WriteLine("Invalid number: {0}", secondStr);
  36.         }
  37.         else if (!int.TryParse(thirdStr, out c))
  38.         {
  39.             Console.WriteLine("Invalid number: {0}", thirdStr);
  40.         }
  41.         else if (!int.TryParse(fourthStr, out d))
  42.         {
  43.             Console.WriteLine("Invalid number: {0}", fourthStr);
  44.         }
  45.         else if (!int.TryParse(fifthStr, out e))
  46.         {
  47.             Console.WriteLine("Invalid number: {0}", fifthStr);
  48.         }
  49.         else
  50.         {
  51.             // Combination with 5 numbers
  52.             if (a + b + c + d + e == 0)
  53.             {
  54.                 flagSum = true;
  55.             }
  56.  
  57.             // Start of combination with 4 numbers
  58.             else if (a + b + c + d == 0)
  59.             {
  60.                 flagSum = true;
  61.             }
  62.             else if (a + b + c + e == 0)
  63.             {
  64.                 flagSum = true;
  65.             }
  66.             else if (a + c + d + e == 0)
  67.             {
  68.                 flagSum = true;
  69.             }
  70.             else if (a + b + d + e == 0)
  71.             {
  72.                 flagSum = true;
  73.             }
  74.             else if (b + c + d + e == 0)
  75.             {
  76.                 flagSum = true;
  77.             }
  78.  
  79.             // Start of combination with 3 numbers
  80.             else if (a + b + c == 0)
  81.             {
  82.                 flagSum = true;
  83.             }
  84.             else if (a + b + d == 0)
  85.             {
  86.                 flagSum = true;
  87.             }
  88.             else if (a + b + e == 0)
  89.             {
  90.                 flagSum = true;
  91.             }
  92.             else if (a + c + d == 0)
  93.             {
  94.                 flagSum = true;
  95.             }
  96.             else if (a + c + e == 0)
  97.             {
  98.                 flagSum = true;
  99.             }
  100.             else if (a + d + e == 0)
  101.             {
  102.                 flagSum = true;
  103.             }
  104.             else if (b + c + d == 0)
  105.             {
  106.                 flagSum = true;
  107.             }
  108.             else if (b + c + e == 0)
  109.             {
  110.                 flagSum = true;
  111.             }
  112.             else if (b + d + e == 0)
  113.             {
  114.                 flagSum = true;
  115.             }
  116.             else if (c + d + e == 0)
  117.             {
  118.                 flagSum = true;
  119.             }
  120.  
  121.             // Start of combination with 2 numbers
  122.             else if (a + b == 0)
  123.             {
  124.                 flagSum = true;
  125.             }
  126.             else if (a + c == 0)
  127.             {
  128.                 flagSum = true;
  129.             }
  130.             else if (a + d == 0)
  131.             {
  132.                 flagSum = true;
  133.             }
  134.             else if (a + e == 0)
  135.             {
  136.                 flagSum = true;
  137.             }
  138.             else if (b + c == 0)
  139.             {
  140.                 flagSum = true;
  141.             }
  142.             else if (b + d == 0)
  143.             {
  144.                 flagSum = true;
  145.             }
  146.             else if (b + e == 0)
  147.             {
  148.                 flagSum = true;
  149.             }
  150.             else if (c + d == 0)
  151.             {
  152.                 flagSum = true;
  153.             }
  154.             else if (c + e == 0)
  155.             {
  156.                 flagSum = true;
  157.             }
  158.             else if (d + e == 0)
  159.             {
  160.                 flagSum = true;
  161.             }
  162.  
  163.             Console.WriteLine("Do we have sum with 0? - {0}", flagSum);
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment