subsetsumrec - Empty set satisfies this target if(target ==...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
public class subsetsumrec { p public static void main(String[] args) { p int[] vals = new int[6]; vals[0] = 2; vals[1] = 5; vals[2] = 12; vals[3] = 7; vals[4] = 9; vals[5] = 6; v for (int i=0; i<42; i++) { if (SS(vals, i, 6, "")) System.out.println("Found subset : "+i); } } public static boolean SS(int[] vals, int target, int length, String numbers)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { // Empty set satisfies this target. if (target == 0) { System.out.println(numbers); return true; } // An empty set can't add up to a non-zero value. if (length == 0) return false; r return SS(vals, target - vals[length-1], length-1, numbers+", "+vals[length-1]) || SS(vals, target, length-1, numbers ); } }...
View Full Document

This document was uploaded on 11/09/2009.

Ask a homework question - tutors are online