forked from adarshpandey10t/Hacktoberfestmine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRod_Cutting.cpp
More file actions
47 lines (35 loc) · 698 Bytes
/
Rod_Cutting.cpp
File metadata and controls
47 lines (35 loc) · 698 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
47
// Rod Cutting Problem
#include<bits/stdc++.h>
using namespace std;
int rod_cut(int price[],int n)
{
int i,j;
int t[n+1][n+1];
for(i=0;i<=n;i++)
t[i][0]=0;
for(j=0;j<=n;j++)
t[0][j]=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i<=j)
t[i][j]=max(price[i-1]+t[i][j-i],t[i-1][j]);
else
t[i][j]=t[i-1][j];
}
}
return t[n][n];
}
int main()
{
int i,n;
cout<<"\nEnter the Size\n";
cin>>n;
int price[n];
cout<<"\nEnter price\n";
for(i=0;i<n;i++)
cin>>price[i];
cout<<"\nMaximum Profit - "<<rod_cut(price,n);
return 0;
}