CBredlow

Untitled

Feb 25th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. static int [] r;
  2. static int [] s;
  3. public static void main(String[] args)
  4. {
  5. int[] p = {1,5,8,9,10,17,17,20,24,30};
  6. int n = 10;
  7. PrintCutRodSolution(p,n);
  8.  
  9. }
  10. public static void ExtendedBottomUpCutRod(int[]p, int n)
  11. {
  12. r = new int[n+1];
  13. s = new int[n+1];
  14. int q;
  15. r[0] = 0;
  16. for(int j =0; j <n; j++)
  17. {
  18. q= -2;
  19. for(int i = 1; i <= j; i++)
  20. {
  21. if(q < p[i]+r[j-i])
  22. {
  23. q=p[i]+r[j-i];
  24. s[j] = i;
  25. }
  26.  
  27. }
  28. r[j] = q;
  29. }
  30. }
  31. public static void PrintCutRodSolution(int [] p , int n)
  32. {
  33. ExtendedBottomUpCutRod(p, n);
  34. while(n>0)
  35. {
  36. System.out.println(s[n]);
  37. n=n-s[n];
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment