Advertisement
dimipan80

Zero Subset

Aug 7th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. /* We are given 5 integer numbers.
  2.  * Write a program that finds all subsets of these numbers whose sum is 0.
  3.  * Assume that repeating the same subset several times is not a problem. */
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class ZeroSubset {
  8.  
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         Scanner scan = new Scanner(System.in);
  12.         System.out.print("Enter a 5 Integer numbers: ");
  13.         int num1 = scan.nextInt();
  14.         int num2 = scan.nextInt();
  15.         int num3 = scan.nextInt();
  16.         int num4 = scan.nextInt();
  17.         int num5 = scan.nextInt();
  18.         scan.close();
  19.  
  20.         boolean foundZeroSubset = false;
  21.         if (num1 + num2 + num3 + num4 + num5 == 0) {
  22.             foundZeroSubset = true;
  23.             System.out.printf("%d + %d + %d + %d + %d = 0\n", num1, num2, num3, num4, num5);
  24.         }
  25.  
  26.         if (num1 + num2 + num3 + num4 == 0) {
  27.             foundZeroSubset = true;
  28.             System.out.printf("%d + %d + %d + %d = 0\n", num1, num2, num3, num4);
  29.         }
  30.  
  31.         if (num1 + num2 + num3 + num5 == 0) {
  32.             foundZeroSubset = true;
  33.             System.out.printf("%d + %d + %d + %d = 0\n", num1, num2, num3, num5);
  34.         }
  35.  
  36.         if (num1 + num2 + num4 + num5 == 0) {
  37.             foundZeroSubset = true;
  38.             System.out.printf("%d + %d + %d + %d = 0\n", num1, num2, num4, num5);
  39.         }
  40.  
  41.         if (num1 + num3 + num4 + num5 == 0) {
  42.             foundZeroSubset = true;
  43.             System.out.printf("%d + %d + %d + %d = 0\n", num1, num3, num4, num5);
  44.         }
  45.  
  46.         if (num2 + num3 + num4 + num5 == 0) {
  47.             foundZeroSubset = true;
  48.             System.out.printf("%d + %d + %d + %d = 0\n", num2, num3, num4, num5);
  49.         }
  50.  
  51.         if (num1 + num2 + num3 == 0) {
  52.             foundZeroSubset = true;
  53.             System.out.printf("%d + %d + %d = 0\n", num1, num2, num3);
  54.         }
  55.  
  56.         if (num1 + num2 + num4 == 0) {
  57.             foundZeroSubset = true;
  58.             System.out.printf("%d + %d + %d = 0\n", num1, num2, num4);
  59.         }
  60.  
  61.         if (num1 + num2 + num5 == 0) {
  62.             foundZeroSubset = true;
  63.             System.out.printf("%d + %d + %d = 0\n", num1, num2, num5);
  64.         }
  65.  
  66.         if (num1 + num3 + num4 == 0) {
  67.             foundZeroSubset = true;
  68.             System.out.printf("%d + %d + %d = 0\n", num1, num3, num4);
  69.         }
  70.  
  71.         if (num1 + num3 + num5 == 0) {
  72.             foundZeroSubset = true;
  73.             System.out.printf("%d + %d + %d = 0\n", num1, num3, num5);
  74.         }
  75.  
  76.         if (num1 + num4 + num5 == 0) {
  77.             foundZeroSubset = true;
  78.             System.out.printf("%d + %d + %d = 0\n", num1, num4, num5);
  79.         }
  80.  
  81.         if (num2 + num3 + num4 == 0) {
  82.             foundZeroSubset = true;
  83.             System.out.printf("%d + %d + %d = 0\n", num2, num3, num4);
  84.         }
  85.  
  86.         if (num2 + num3 + num5 == 0) {
  87.             foundZeroSubset = true;
  88.             System.out.printf("%d + %d + %d = 0\n", num2, num3, num5);
  89.         }
  90.  
  91.         if (num2 + num4 + num5 == 0) {
  92.             foundZeroSubset = true;
  93.             System.out.printf("%d + %d + %d = 0\n", num2, num4, num5);
  94.         }
  95.  
  96.         if (num3 + num4 + num5 == 0) {
  97.             foundZeroSubset = true;
  98.             System.out.printf("%d + %d + %d = 0\n", num3, num4, num5);
  99.         }
  100.  
  101.         if (num1 + num2 == 0) {
  102.             foundZeroSubset = true;
  103.             System.out.printf("%d + %d = 0\n", num1, num2);
  104.         }
  105.  
  106.         if (num1 + num3 == 0) {
  107.             foundZeroSubset = true;
  108.             System.out.printf("%d + %d = 0\n", num1, num3);
  109.         }
  110.  
  111.         if (num1 + num4 == 0) {
  112.             foundZeroSubset = true;
  113.             System.out.printf("%d + %d = 0\n", num1, num4);
  114.         }
  115.  
  116.         if (num1 + num5 == 0) {
  117.             foundZeroSubset = true;
  118.             System.out.printf("%d + %d = 0\n", num1, num5);
  119.         }
  120.  
  121.         if (num2 + num3 == 0) {
  122.             foundZeroSubset = true;
  123.             System.out.printf("%d + %d = 0\n", num2, num3);
  124.         }
  125.  
  126.         if (num2 + num4 == 0) {
  127.             foundZeroSubset = true;
  128.             System.out.printf("%d + %d = 0\n", num2, num4);
  129.         }
  130.  
  131.         if (num2 + num5 == 0) {
  132.             foundZeroSubset = true;
  133.             System.out.printf("%d + %d = 0\n", num2, num5);
  134.         }
  135.  
  136.         if (num3 + num4 == 0) {
  137.             foundZeroSubset = true;
  138.             System.out.printf("%d + %d = 0\n", num3, num4);
  139.         }
  140.  
  141.         if (num3 + num5 == 0) {
  142.             foundZeroSubset = true;
  143.             System.out.printf("%d + %d = 0\n", num3, num5);
  144.         }
  145.  
  146.         if (num4 + num5 == 0) {
  147.             foundZeroSubset = true;
  148.             System.out.printf("%d + %d = 0\n", num4, num5);
  149.         }
  150.  
  151.         if (num1 == 0) {
  152.             foundZeroSubset = true;
  153.             System.out.printf("%d\n", num1);
  154.         }
  155.  
  156.         if (num2 == 0) {
  157.             foundZeroSubset = true;
  158.             System.out.printf("%d\n", num2);
  159.         }
  160.  
  161.         if (num3 == 0) {
  162.             foundZeroSubset = true;
  163.             System.out.printf("%d\n", num3);
  164.         }
  165.  
  166.         if (num4 == 0) {
  167.             foundZeroSubset = true;
  168.             System.out.printf("%d\n", num4);
  169.         }
  170.  
  171.         if (num5 == 0) {
  172.             foundZeroSubset = true;
  173.             System.out.printf("%d\n", num5);
  174.         }
  175.  
  176.         if (!foundZeroSubset) {
  177.             System.out.println("No Zero Subset!");
  178.         }
  179.     }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement