子集和问题

Given an integer array, find pairs in an array which sum up to a given number.

For example: Array{4,5,1,3,2} and required sum=6 then output should be [1,5] and [2,4].


package com.zhuyu_deng.test;



public class Test
{
	private static int a[] = new int[] {1, 2, 3, 4, 5, 6};
	private static int len = a.length - 1;
	private static int x[] = new int[a.length];
	private static boolean flag;
	private static int C = 6;
	
	public static void explore()
	{
		for (int i = 0; i < a.length; ++i)
			System.out.print(a[i] + " ");
		System.out.println(C);
		traceback(0);
	}
	
	private static void traceback(int k)
	{
		if (k > len)
		{
			if (isRight(len))
			{
				print();
			}
		}
		else
		{
			for (int i = 0; i < 2; ++i)
			{
				x[k] = i;
				if (isPartial(k))
					traceback(k + 1);
			}
		}
	}

	private static boolean isRight(int n)
	{
		int sum = 0;
		for (int i = 0; i <= n; ++i)
		{
			sum += a[i] * x[i];
		}
		if (sum == C)
			return true;
		else
			return false;
	}
	
	private static boolean isPartial(int n)
	{
		int sum = 0;
		for (int i = 0; i <= n; ++i)
		{
			sum += a[i] * x[i];
		}
		if (sum <= C)
			return true;
		else
			return false;
	}
	
	private static void print()
	{
		for (int i = 0; i < a.length; ++i)
		{
			if (x[i] == 1)
				System.out.print(a[i] + " ");
		}
		System.out.println();
	}
	public static void main(String args[])
	{
		// int[] a = {-2,11,-4,13,-5,-2};
		int[][] b = { { 0, -2, -7, 0 }, { 9, 2, -6, 2 }, { -4, 1, -4, 1 },
				{ -1, 8, 0, -2 } };
		int [][] matrix = 
			{	{2,3,4,1},
				{1,1,3,9},
				{2,2,3,1},
				{2,2,3,1}
			};
		
		explore();
	}
}



public class subSet
{
	public Object a;
	public int n;
	public int[] x;
	public boolean flag;
	private int c;

	public subSet(int[] a, int c)
	{
		this.a = a;
		this.c = c;
		this.n = a.length;
		this.flag = false;
		this.x = new int[n];
	}

	public boolean isPartial(int k)
	{
		int sum = 0;
		
		for (int i = 0; i < k; i++)
			sum += ((int[]) (a))[i] * x[i];
		return sum <= c;
	}

	public boolean isComplete(int k)
	{
		int sum = 0;
		
		if (k >= n)
		{
			for (int i = 0; i < n; i++)
				sum += ((int[]) (a))[i] * x[i];
		}
		return sum == c;
	}

	public void printSolution(int k)
	{
		for (int i = 0; i < n; i++)
			if (x[i] == 1)
				System.out.print(((int[]) (a))[i] + " ");
		System.out.println();
	}

	public static void backtrack(subSet p)
	{
		explore(p, 0);
		if (!p.flag)
			System.out.println("no sulution!");
	}

	private static void explore(subSet p, int k)
	{
		if (k >= p.n)
		{
			if (p.isComplete(k))
			{
package com.zhuyu_deng.test;
public class Test
{
//	public Object a;
	private int a[];
	private int size;  // 记录个数
	private int[] x;
	private boolean flag;
	private int val;

	public Test(int[] a, int c)
	{
		this.a = a;
		this.val = c;
		this.size = a.length;
		this.flag = false;
		this.x = new int[size];
	}

	public boolean isPartial(int k)
	{
		int sum = 0;
		
		for (int i = 0; i < k; i++)
			sum += a[i] * x[i];
		return sum <= val;
	}

	public boolean isComplete(int k)
	{
		int sum = 0;
		
		if (k >= size)
		{
			for (int i = 0; i < size; i++)
				sum += a[i] * x[i];
		}
		return sum == val;
	}

	public void printSolution(int k)
	{
		for (int i = 0; i < size; i++)
			if (x[i] == 1)
				System.out.print(a[i] + " ");
		System.out.println();
	}

	public static void backtrack(Test p)
	{
		explore(p, 0);
		if (!p.flag)
			System.out.println("no sulution!");
	}

	private static void explore(Test p, int k)
	{
		if (k >= p.size)
		{
			if (p.isComplete(k))
			{
				p.flag = true;
				p.printSolution(k);
			}
			return;
		}
		for (int i = 0; i < 2; i++)
		{
			p.x[k] = i;
			if (p.isPartial(k))
				explore(p, k + 1);
		}
	}
	public static void main(String args[])
	{
		int b[] = { 1, 2, 3, 4, 5, 6};
		Test t = new Test(b, 6);
		t.backtrack(t);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值