动态规划cuttingRod

算法导论上的。

naive的algorithm:

#include 
#include 
using namespace std;
int max(int a, int b)
{  return (a>b)?a:b;
}
int cutRod(int price[], int n)
{
  if(n<=0){ return 0;}
  int max_val = INT_MIN;
  for (int i = 0;i

top-down algorithm:

#include 

#include 

using namespace std;

int cut2(int price[],int n, int rem[]);

int max(int a, int b){
  return (a>b)?a:b;
}

int cut1(int price[],int n){
  int rem[n];
   for(int i = 0;i<=n;i++){
    rem[i] = INT_MIN;
   }
   return cut2(price,n,rem);
}

int cut2(int price[],int n, int rem[]){
  int q = INT_MIN;
  if(rem[n]>=0){
    return rem[n];
  }
  if(n == 0){
    q = 0;
  }
  else{
    q = INT_MIN;
    for(int i = 0;i

你可能感兴趣的:(动态规划cuttingRod)