sum of recursive

Terminal Transcript @ Sep 15, 2012 10:34:04 PM

1 import java.util.ArrayList;
2 import java.util.Arrays;
3
4 class SumSet {
5 static void sum_up_recursive(ArrayList<Integer> numbers, int target, ArrayList<Integer> partial)
6 {
7 int s = 0;
8 for (int x: partial)
9 s += x;
10 if (s == target)
11 System.out.println("sum("+Arrays.toString(partial.toArray())+")="+target);
12 if (s >= target)
13 return;
14 for(int i=0;i<numbers.size();i++)
15 {
16 ArrayList<Integer> remaining = new ArrayList<Integer>();
17 int n = numbers.get(i);
18 for (int j=i+1; j<numbers.size();j++)
19 remaining.add(numbers.get(j));
20 ArrayList<Integer> partial_rec = new ArrayList<Integer>(partial);
21 partial_rec.add(n);
22 sum_up_recursive(remaining,target,partial_rec);
23 }
24 }
25 static void sum_up(ArrayList<Integer> numbers, int target)
26 {
27 sum_up_recursive(numbers,target,new ArrayList<Integer>());
28 }
29 public static void main(String args[])
30 {
31 Integer[] numbers = {3,9,8,4,5,7,10,12, 14, 13, 18};
32 int target = 21;
33 sum_up(new ArrayList<Integer>(Arrays.asList(numbers)),target);
34 }
35 }
36
SumSet.java 32,17-23 Bot
terminal++@10.0.0.8:~/programming/recursive$ ./builder.sh
Clean up
First compile the java
USER CLASS PATH : .
SOURCE CLASS PATH : .
[parsing started RegularFileObject[SumSet.java]]
[parsing completed 30ms]
[search path for source files: .]
[search path for class files: /data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar,/system/framework/core.jar,/system/framework/core-junit.jar,/system/framework/bouncycastle.jar,/system/framework/ext.jar,/system/framework/framework.jar,/system/framework/android.policy.jar,/system/framework/services.jar,/system/framework/apache-xml.jar,/system/framework/filterfw.jar,.]

[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/util/ArrayList.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/util/Arrays.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/Object.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/Integer.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/String.class)]]
[checking SumSet]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/Number.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/util/AbstractList.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/util/AbstractCollection.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/Iterable.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/util/Collection.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/Byte.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/Character.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/Short.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/System.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/io/PrintStream.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/io/FilterOutputStream.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/io/OutputStream.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/util/List.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/util/Iterator.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/Long.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/Float.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/Double.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/Boolean.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/Void.class)]]
[loading ZipFileIndexFileObject[/data/data/com.spartacusrex.spartacuside/files/system/classes/android.jar(java/lang/StringBuilder.class)]]
1 import java.util.ArrayList; 2 import java.util.Arrays;
3 4 class SumSet {
5 static void sum_up_recursive(ArrayList<Integer> numbers, int target, ArrayList<Integer> partial) 6 {
7 int s = 0; 8 for (int x: partial)
9 s += x; 10 if (s == target)
11 System.out.println("sum("+Arrays.toString(partial.toArray())+")="+target);
12 if (s >= target)
13 return;
14 for(int i=0;i<numbers.size();i++)
15 {
16 ArrayList<Integer> remaining = new ArrayList<Integer>();
17 int n = numbers.get(i);
18 for (int j=i+1; j<numbers.size();j++)
19 remaining.add(numbers.get(j));
20 ArrayList<Integer> partial_rec = new ArrayList<Integer>(partial);
21 partial_rec.add(n);
22 sum_up_recursive(remaining,target,partial_rec);
23 }
24 }
25 static void sum_up(ArrayList<Integer> numbers, int target)
26 {
27 sum_up_recursive(numbers,target,new ArrayList<Integer>());
28 }
29 public static void main(String args[])
SumSet.java 1,1 Top
"SumSet.java" 36L, 1059Cc

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