forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrod_cutting.cpp
More file actions
46 lines (32 loc) · 728 Bytes
/
rod_cutting.cpp
File metadata and controls
46 lines (32 loc) · 728 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<climits>
using namespace std;
int rodCutting(int n, int value[])
{
int i,j;
int result[n+1];
result[0]=0;
for(i=1;i<=n;i++)
{
result[i]=INT_MIN;
for(j=0;j<i;j++)
{
result[i]=max(result[i],value[j]+result[i-(j+1)]);
}
}
return result[n];
}
int main()
{
int n;
cout<<"Enter the length of the rod"<<endl;
cin>>n;
int value[n];
cout<<"Enter the values of pieces of rod of all size"<<endl;
for(int i=0;i<n;i++)
cin>>value[i];
cout<<"Maximum obtainable value by cutting up the rod in many pieces are"<<endl;
cout<<rodCutting(n,value);
cout<<endl;
return 0;
}