Max rope cutting
PROBLEM :
Given a rope of length n meters, cut the rope in different parts of integer lengths in a way that maximizes product of lengths of all parts. You must make at least one cut.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is length of rope denoted by N.
Output:
Print the maximizes product of lengths of all parts.
Constraints:
1 = T = 50
1 = N = 100
Example:
Input:
2
2
5
Output:
1
6
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Using Auxilary arry)
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
long long Max_rope_cutting(int ) ;
int main()
{
int t,no ;
long long ans ;
cin>>t ;
while(t--)
{
cin>>no ;
ans=Max_rope_cutting(no) ;
cout<<ans<<endl ;
}
return 0;
}
long long Max_rope_cutting(int no)
{
long long multi=1 ;
if(no==1||no==2||no==3)
return no-1 ;
while(no>4)
{
no=no-3 ;
multi=multi*3 ;
}
multi=multi*no ;
return multi ;
}
---------------------------------------------------------------------------------
Given a rope of length n meters, cut the rope in different parts of integer lengths in a way that maximizes product of lengths of all parts. You must make at least one cut.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is length of rope denoted by N.
Output:
Print the maximizes product of lengths of all parts.
Constraints:
1 = T = 50
1 = N = 100
Example:
Input:
2
2
5
Output:
1
6
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Using Auxilary arry)
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
long long Max_rope_cutting(int ) ;
int main()
{
int t,no ;
long long ans ;
cin>>t ;
while(t--)
{
cin>>no ;
ans=Max_rope_cutting(no) ;
cout<<ans<<endl ;
}
return 0;
}
long long Max_rope_cutting(int no)
{
long long multi=1 ;
if(no==1||no==2||no==3)
return no-1 ;
while(no>4)
{
no=no-3 ;
multi=multi*3 ;
}
multi=multi*no ;
return multi ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment