-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRec_rope_problem.cpp
More file actions
34 lines (34 loc) · 858 Bytes
/
Rec_rope_problem.cpp
File metadata and controls
34 lines (34 loc) · 858 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
/*Given a rope of length n, you need to find maximum number of
pieces you can make such that length of every piece is in the set{a,b,c}
for given three values of {a,b,c}*/
#include <iostream>
using namespace std;
int max(int a,int b, int c)
{
if(a>b&&a>c)
return a;
else if(b>a&&b>c)
return b;
else
return c;
}
int maxcuts(int n,int a,int b,int c)
{
if(n==0)
return 0;
if(n<0)
return -1;
int res = max(maxcuts(n-a,a,b,c),maxcuts(n-b,a,b,c),maxcuts(n-c,a,b,c));
if(res == -1)
return -1;
return res+1;
}
int main()
{ int n,r1,r2,r3;
cout<<"\n\tEnter the size of the rope : ";
cin>>n;
cout<<"\n\tEnter the length of rope pieces : ";
cin>>r1>>r2>>r3;
cout<<"\n\tThe no of pieces are : "<<maxcuts(n,r1,r2,r3);
return 0;
}