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); } }


LinkBack URL
About LinkBacks





Reply With Quote


Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum