JAVA PROGRAM Modify the memoizedCutRod method to keep track of the rod length th
ID: 647312 • Letter: J
Question
JAVA PROGRAM
Modify the memoizedCutRod method to keep track of the rod length that generates the
optimal solution for each of the rod lengths.
? Add a column to the output table to show the optimal rod cut results ? the lengths of the
rods (after the cuts) that yield the optimal revenue values
? The output table should now have these columns with headers:
Length , Rev CR, Num Calls CR, Rev MCR, Num Calls MCR, Cut Pieces
The output from running the program should display on the screen
as follows:
Explanation / Answer
import java.io.*;
import java.util.*;
public class MemorizedCutRod
{
public static double cutRod(double[] p, int n)
{
if(n==0) return 0;
double q=-1000;
for(int i=0;i<n;i++)
{
q=Math.max(q,p[i]+cutRod(p,n-i-1));
System.out.println(q);
}
return q;
}
public static void main(String[] args)
{
double[] p={1,5,8,9,10,17,17,20,24,30};
double val = cutRod(p,10);
System.out.println(val);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.