Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-19-2007, 03:01 AM
Member
 
Join Date: Nov 2007
Posts: 1
scts102 is on a distinguished road
Help with recursion
Hello. I need some help with recursion. Given an array of integers, I have to return the sum of integers that is closest to goalWeight without going over. My code works for some test sets, but not all. I was wondering if someone could take a look at my code and tell me what I am doing wrong...


Note: vars with and without represent calls made, including and excluding A[first], respectively. first and last represent the first and last elements in the array.
Code:
public int maxPack(int[] A, int first, int last, int goalWeight) { int maxWeight = 0; int with=0; int without = 0; if(goalWeight <=0) return 0; if(first == last){ if(A[first] <= goalWeight) maxWeight += A[first]; return maxWeight; } else{ if (A[first] <= goalWeight) { with += A[first]; with += maxPack(A, first + 1, last, (goalWeight - A[first])); } without += maxPack(A, first + 1, last, goalWeight); if(with < goalWeight || without < goalWeight){ if(without < goalWeight){ if(with > without) { maxWeight += with; return maxWeight; } else { maxWeight += without; return maxWeight; } } else{ assert(with < goalWeight); maxWeight += with; return maxWeight; } } return 0; } }
Thanks in advance!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-20-2007, 12:07 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.util.*; public class Recurse { static Random seed = new Random(); public static void main(String[] args) { int limit = 16; int size = 10; int maxVal = 25; for(int j = 0; j < 5; j++) { int[] n = getArray(limit, size); System.out.printf("n = %s maxVal = %d%n", Arrays.toString(n), maxVal); System.out.printf(" getMax() = %d%n", getMax(n, maxVal, 0, 0)); System.out.printf(" getSum() = %d%n", getSum(n, maxVal)); System.out.printf("maxPack() = %d%n", maxPack(n, 0, size-1, maxVal)); System.out.println("----------"); } } private static int getMax(int[] array, int maxValue, int index, int value) { if(value + array[index] <= maxValue) value += array[index]; else return value; if(index == array.length-1) return value; return getMax(array, maxValue, index+1, value); } private static int getSum(int[] array, int goalWeight) { int weight = 0; for(int j = 0; j < array.length; j++) { if(weight + array[j] > goalWeight) break; weight += array[j]; } return weight; } private static int maxPack(int[] A, int first, int last, int goalWeight) { int maxWeight = 0; int with = 0; int without = 0; if(goalWeight <= 0) return 0; if(first == last){ if(A[first] <= goalWeight) maxWeight += A[first]; return maxWeight; } else{ if (A[first] <= goalWeight) { with += A[first]; with += maxPack(A, first + 1, last, (goalWeight - A[first])); } without += maxPack(A, first + 1, last, goalWeight); if(with < goalWeight || without < goalWeight){ if(without < goalWeight){ if(with > without) { maxWeight += with; return maxWeight; } else { maxWeight += without; return maxWeight; } } else{ assert(with < goalWeight); maxWeight += with; return maxWeight; } } return 0; } } private static int[] getArray(int limit, int size) { int[] n = new int[size]; Arrays.fill(n, limit+1); int count = 0; while(count < size) { int candidate = seed.nextInt(limit+1); if(!contains(n, candidate)) n[count++] = candidate; } return n; } private static boolean contains(int[] array, int target) { for(int j = 0; j < array.length; j++) { if(array[j] == target) return true; } return false; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help With Recursion andrew777 New To Java 1 03-29-2008 02:51 PM
recursion kdeighan New To Java 3 01-25-2008 11:48 PM
Recursion bozovilla Advanced Java 3 01-07-2008 06:53 PM
recursion ravian New To Java 2 12-03-2007 07:15 PM
Recursion in java lenny Advanced Java 1 08-07-2007 08:23 AM


All times are GMT +3. The time now is 03:26 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org