forked from MainakRepositor/500-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b90840
commit eec44e3
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// A Naive recursive solution for Rod cutting problem | ||
#include<stdio.h> | ||
#include<limits.h> | ||
|
||
// A utility function to get the maximum of two integers | ||
int max(int a, int b) { return (a > b)? a : b;} | ||
|
||
/* Returns the best obtainable price for a rod of length n and | ||
price[] as prices of different pieces */ | ||
int cutRod(int price[], int n) | ||
{ | ||
if (n <= 0) | ||
return 0; | ||
int max_val = INT_MIN; | ||
|
||
// Recursively cut the rod in different pieces and compare different | ||
// configurations | ||
for (int i = 0; i<n; i++) | ||
max_val = max(max_val, price[i] + cutRod(price, n-i-1)); | ||
|
||
return max_val; | ||
} | ||
|
||
/* Driver program to test above functions */ | ||
int main() | ||
{ | ||
int arr[] = {1, 5, 8, 9, 10, 17, 17, 20}; | ||
int size = sizeof(arr)/sizeof(arr[0]); | ||
printf("Maximum Obtainable Value is %dn", cutRod(arr, size)); | ||
getchar(); | ||
return 0; | ||
} |