Advertisement
bkit4s0

[cut-rod] Recursive top-down implementation

May 10th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.41 KB | None | 0 0
  1. import java.io.IOException;
  2.  
  3. class type {
  4.  
  5.     static int cut_rod(int p[], int n) {
  6.         if (n == 0)
  7.             return 0;
  8.         int q = -1;
  9.         for (int i = 1; i <= n; i++)
  10.             q = Math.max(q, p[i] + cut_rod(p, n - i));
  11.         return q;
  12.     }
  13.  
  14.     public static void main(String args[]) throws NumberFormatException,
  15.             IOException {
  16.         int[] p = {0,1,5,8,9,10,17,17,20,24,30};
  17.         int r = 4;
  18.         System.out.println(cut_rod(p,r));
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement