联合包含所有元素和交集的两个子集不包含任何元素

时间:2015-11-03 02:52:55

标签: java linked-list

单个链接列表与它们出现的顺序相同。然后在不更改链接列表的情况下,程序应该打印是否存在列表的分区(两个子集的并集包含所有元素,并且交集不包含当XOR&#d; d等于目标值时,其总和。示例:-4 3 5 {{-4} {3,5},{ - 4,3} {5},{ - 4,5} {3},{ - 4,3,5} {0}}总和:{{ - 4} {8},{ - 1} {5},{1} {3},{4} {0}}总和的数量:{ - 12,-6,2,4}假定目标是2,因此它应该打印为真。

enter code here
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.*;

public class Equal {
static int a;
  public static void main(String[] args) throws IOException {
    Scanner sc = new Scanner(new File("testing.txt"));
    Scanner in = new Scanner(System.in);
    sc.useDelimiter(" ");
    LinkedList ll = new LinkedList();
    while (sc.hasNext()) {
      String s = sc.next();
      /*if (s.trim().isEmpty()) {
        continue;
      }*/
      int b=0;

          ll.add(b, s);


     // System.out.println(s);
    b++;
    }
   //System.out.println("Original contents of ll: " + ll);
    System.out.println(ll.size());
    sc.close();
   System.out.println("Enter an integer");
    a = in.nextInt();
    String d ;
    int e=0;
    int z=0;
   /* int[] binary = new int[(int) Math.pow(2, N)];
    for (int i = 0; i < Math.pow(2, N); i++) 
    {
        int b = 1;
        binary[i] = 0;
        int num = i, count = 0;
        while (num > 0) 
        {
            if (num % 2 == 1)
                count++;
            binary[i] += (num % 2) * b;
            num /= 2;
            b = b * 10;
        }

        if (count == 1) 
        {
            System.out.print("{ ");
            for (int j = 0; j < N; j++) 
            {
                if (binary[i] % 10 == 1)
                    System.out.print(ll+ " ");
                binary[i] /= 10;
            }
            System.out.println("}");
        }
    }*/
    sc.close();
  int N=ll.size();
    /*set_size of power set of a set with set_size
    n is (2**n -1)*/
     //double pow_set_size =Math.pow(2, N);
    int counter, j;

    /*Run from counter 000..0 to 111..1*/
    for(counter = 0; counter < N; counter++)
    {
    for(j = 0; j < N; j++)
    {
        if(counter!=j)
        {


            //System.out.println( ll.get(counter)+"{"+ll.get(j)+"}");
        }
        if(counter!=j)
        {
         d=String.valueOf( ll.get(j));

         e=Integer.parseInt(d);
         System.out.println("printing subset values "+e);
         if(counter==j)
         {
        z =z+e;
         }
        }
    }
    String b=String.valueOf( ll.get(counter));
    int f=Integer.parseInt(b);

    int g=e-f;

    if(g==a)
    {
        System.out.println("true");
    }

    }
  }

  }

0 个答案:

没有答案