-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrodcut.cpp
59 lines (58 loc) · 1.05 KB
/
rodcut.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void cutrod(int* price,int len)
{
int profit[len+1][len+1];
int i,j;
for(i=0;i<=len;i++)
{
profit[0][i]=0;
}
for(i=0;i<=len;i++)
{
profit[i][0]=0;
}
for(i=1;i<=len;i++)
{
profit[1][i]=i*price[0];
}
for(i=2;i<=len;i++)
{
for(j=1;j<=len;j++)
{
if(i>j)
{
profit[i][j]=profit[i-1][j];
}
else if(i==j)
{
profit[i][j]=max(profit[i-1][j],price[i-1]);
}
else
{
profit[i][j]=max(profit[i-1][j],profit[i][i]+profit[i][j-i]);
}
}
}
for(i=0;i<=len;i++)
{
for(j=0;j<=len;j++)
{
cout<<profit[i][j]<<" ";
}
cout<<"\n";
}
}
int main()
{
int len,i;
cin>>len;
int price[len];
for(i=0;i<len;i++)
{
cin>>price[i];
}
cutrod(price,len);
return 0;
}