title: how to solve 0-1 package problem by iteration?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1549943/design-patterns-for-converting-recursive-algorithms-to-iterative-ones">Design patterns for converting recursive algorithms to iterative ones</a>  </p>
  </blockquote>
  
  
  
  <p>I can only figure out a recursive way:
  how to transform it into iteration?
  for simplicity, my recursive way only find the result value.</p>
  
  <pre><code>#include&lt;iostream&gt;
  //0-1 bag
  using namespace std;
  int weight[5]={50,30,45,25,5};
  int value[5]={200,180,225,200,50};
  
  int valueAfterTake(int objectNO, int bagSpaceLeft)
  {
      int take,notTake;
      if(objectNO &lt; 0)
      {
          return 0;
      }
      else
      {
          take = value[objectNO] + valueAfterTake(objectNO - 1,bagSpaceLeft - weight[objectNO]);
          notTake = valueAfterTake(objectNO - 1, bagSpaceLeft);
      }
      if(weight[objectNO] &gt; bagSpaceLeft)
      {
          return notTake;
      }
  
      if(take &gt; notTake)
      {
          return take;
      }
      else
      {
          return notTake;
      }
  }
  
  
  int main()
  {
      cout&lt;&lt;valueAfterTake(4,100)&lt;&lt;endl;
      return 0;
  }
  </code></pre>