Word

Terminal Transcript @ Sep 19, 2012 9:57:02 PM

terminal++@10.0.0.2:~$ ls
bin lib programming projects sdcard sum.java system tmp
terminal++@10.0.0.2:~$ cd programming/
terminal++@10.0.0.2:~/programming$ ls
8Queens aandb matrixRotate90Deg replaceSpace vpnConnection.sh
Book Solutions hello recursive trie
terminal++@10.0.0.2:~/programming$ cd re
recursive/ replaceSpace/
terminal++@10.0.0.2:~/programming$ cd recursive/
terminal++@10.0.0.2:~/programming/recursive$ ls
SumSet.class SumSet.jar SumSet.java builder.sh log.txt run.sh
terminal++@10.0.0.2:~/programming/recursive$ cat SumSet.java
import java.util.ArrayList;
import java.util.Arrays;

class SumSet {
static void sum_up_recursive(ArrayList<Integer> numbers, int target, ArrayList<Integer> partial)
{
int s = 0;
for (int x: partial)
s += x;
if (s == target)
System.out.println("sum("+Arrays.toString(partial.toArray())+")="+target);
if (s >= target)
return;
for(int i=0;i<numbers.size();i++)
{
ArrayList<Integer> remaining = new ArrayList<Integer>();
int n = numbers.get(i);
for (int j=i+1; j<numbers.size();j++)
remaining.add(numbers.get(j));
ArrayList<Integer> partial_rec = new ArrayList<Integer>(partial);
partial_rec.add(n);
sum_up_recursive(remaining,target,partial_rec);
}
}
static void sum_up(ArrayList<Integer> numbers, int target)
{
sum_up_recursive(numbers,target,new ArrayList<Integer>());
}
public static void main(String args[])
{
Integer[] numbers = {3,9,8,4,5,7,10,12, 14, 13, 18};
int target = 21;
sum_up(new ArrayList<Integer>(Arrays.asList(numbers)),target);
}
}

terminal++@10.0.0.2:~/programming/recursive$

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s