Advertisement
TzvetanIG

ZeroSubset

Mar 20th, 2014
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class ZeroSubset
  5. {
  6.     static void Main()
  7.     {
  8.         string ConsoleString = Console.ReadLine();
  9.         string[] numbersLikeString = ConsoleString.Split(' ');
  10.         int[] numbers = new int[5];
  11.        
  12.         for (int i = 0; i < 5; i++)
  13.         {
  14.             numbers[i] = int.Parse(numbersLikeString[i]);
  15.         }
  16.  
  17.         if (numbers.Sum() == 0)
  18.         {
  19.             Console.WriteLine("0 + 0 + 0 + 0 + 0 = 0");
  20.             return;
  21.         }
  22.  
  23.         long sum = 0;
  24.         string result = "";
  25.         bool isZeroSubset = false;
  26.  
  27.        
  28.         for (int combination = 1; combination < 32; combination++)
  29.         {
  30.             int[] bits = new int[5];
  31.  
  32.             for (int i = 0; i < 5; i++)
  33.         {
  34.                 bits[i] = (combination >> i) & 1;
  35.                 sum += numbers[i] * bits[i];
  36.                 if (bits[i] == 1)
  37.                 {
  38.                     result += numbers[i] + " + ";
  39.                 }
  40.         }
  41.  
  42.             result = result.Trim('+', ' ');
  43.             result += " = " + sum;
  44.  
  45.             if (sum == 0)
  46.             {
  47.                 Console.WriteLine(result);
  48.                 isZeroSubset = true;
  49.             }
  50.  
  51.             result = "";
  52.             sum = 0;
  53.         }
  54.  
  55.         if (isZeroSubset == false)
  56.         {
  57.             Console.WriteLine("no zero subset");
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement