I'm working on this problem here. I know that it is a recursive problem, and I got my function together, and I think that I am calling the function properly.
However when I run my program it only outputs 3 0's. Any ideas as to why?
Code:import java.util.*; import java.io.*; public class _4 { static int nMAXSIZE, nBEST = 0, nNUMTRACKS; static int[] arnSize, arnUtil; public static void main(String[] args) throws IOException { Scanner sin = new Scanner(new FileReader("DATA4.txt")); PrintWriter fout = new PrintWriter(new FileWriter("OUT4.txt")); int nAlbums = 0; int nSpace = 0; while (sin.hasNextInt()) { nMAXSIZE = 0; nSpace = sin.nextInt(); nAlbums = sin.nextInt(); arnSize = new int[nAlbums]; arnUtil = new int[nAlbums]; for (int i=0;i<nAlbums;i++) { arnSize[i] = sin.nextInt(); arnUtil[i] = sin.nextInt(); recurse(0,0,0); } System.out.println(nBEST); } fout.close(); sin.close(); } public static void recurse(int nAlbum,int nUtil,int nSpace) { if (nSpace > nMAXSIZE) { return; } if (nAlbum == nNUMTRACKS) { return; } nUtil += arnUtil[nAlbum]; nSpace += arnSize[nAlbum]; if (nUtil > nBEST) { nBEST = nUtil; } recurse(nAlbum+1,nUtil,nSpace); } }
nNUMTRACKS == 0, so on the first call nAlbum == 0 and you exit.
Also, this isn't a recursion problem, it's a linear programming problem.
Well our instructor said it was recursive. :s Why would he say that?
Oh thanks.![]()
Probably because linear programming is a senior/graduate level math course in college. You can attempt to solve it with recursion, but it's going to be more of a trial and error type thing.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks