Advertisement
Riz1Ahmed

Rod Cutting (Top Down)

Feb 19th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. #include <cstdio>
  2. #include <climits>
  3. #include <algorithm>
  4. using namespace std;
  5. int r[10000];
  6. int RodCut(int p[],int n){
  7.     if (r[n]) return r[n];
  8.     r[n]=p[n];
  9.     for (int i=1; i<n; i++)
  10.         r[n]=max(r[n],RodCut(p,i)+RodCut(p,n-i));
  11.     return r[n];
  12. }
  13. using namespace std;
  14. int main(){
  15.     int len[]={0,1,2,3,4,5,6,7,8,9,10};
  16.     int p[]={0,1,5,8,9,10,17,17,20,24,30};
  17.     int n=sizeof(p)/sizeof(p[0])-1;
  18.     r[n]=p[n];
  19.     r[n]=max(r[n],RodCut(p,1)+RodCut(p,n-1));
  20.     for(int i=1; i<n; i++)
  21.         printf("%d ",r[i]);
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement