동적 기획 문제: 강철 줄 절단

1290 단어
밑에서 아래로 위조 코드
  BOTTOM-UP-CUT-ROD(p,n)
 let r[0..n]be a new array
 r[0]=0
 for j=1 to n
   q=-1
   for i=1 to j
       q=max(q,p[i]+r[j-i]) 
       r[j]=q
   return r[n]

이루어지다
<!-- lang: cpp -->
#include <iostream>
#include <vector>

using namespace std;

 void print_s(vector<int> s, int n);
 vector<int> Bottom_Up_Cut_Rod(vector<int> price, int n);
 int main()
 {
 int i = 0;
 int a[11] = {0,1,5,8,9,10,17,17,20,24,30};
 vector<int> price;
 while(i < 11)
 {
     price.push_back(a[i]);
     i++;
 }

 vector<int> s(Bottom_Up_Cut_Rod(price, 8));
 cout<<"The best cut :";
 print_s(s, 8);
 return 0;
 }

 vector<int> Bottom_Up_Cut_Rod(vector<int> price, int n)
 {
 vector<int> result(n+1, -1);
 vector<int> s(n+1, -1);
 int i, j, q;
 result[0] = 0;
 s[0] = 0;

 for(i = 1; i < n+1; i++)
 {
     q = -1;
     for(j = 1; j < i+1; j++)
         if(q < price[j]+result[i-j])
         {
            q = price[j] + result[i-j];
            s[i] = j;
         }
     result[i] = q;
    }
}

Reference
[1].http://blog.csdn.net/duxingstar/article/details/6032411
[2].http://www.2cto.com/kf/201405/302344.html
[3].알고리즘 도론 204~210

좋은 웹페이지 즐겨찾기