int main() { printf("max value = %u\n", cutrod(price, 6)); printf("max value = %u\n", cutrod(price, 10)); return 0; }
int main() { /// the first entry is a dummy value so we iterate from 1. uint32_t price[] = {0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30}; printf("max value = %u\n", cutrod(price, 6)); printf("max value = %u\n", cutrod(price, 10)); return 0; }
int main() { int a[11]={1,5,8,9,10,17,17,20,24,30}; int ans=cutrod(a,15); printf("the maximum revenue is %d\n",ans); return 0; }
int cutrod(int *a,int n) { int i,ans; if(n==0) { return 0; } ans=0; for(i=1;i<=n;i++) { ans=max(ans,a[i-1]+cutrod(a,n-i)); } printf("%d\n",ans); return ans; }
uint32_t cutrod(uint32_t price[], uint32_t ninches) { uint32_t maxvalue = 0; if (ninches == 0) return 0; // notice that we iterate from 1 because we first cut a 1 inch part // and add it to the max value for the rest (ninchines - 1). for (unsigned i=1; i<=ninches; ++i) { maxvalue = std::max(maxvalue, price[i]+ cutrod(price, ninches-i)); } return maxvalue; }
uint32_t cutrod(const uint32_t price[], const uint32_t n) { uint32_t maxvalue = 0; if (revenues[n]) return revenues[n]; if (n == 0) return 0; // notice that we iterate from 1 because we first cut a 1 inch part // and add it to the max value for the rest (n - 1). for (unsigned i=1; i<=n; ++i) { maxvalue = std::max(maxvalue, price[i]+ cutrod(price, n-i)); } revenues[n] = maxvalue; return maxvalue; }