Question:
Mr. Xhas recently shut down his factory and wants to sell off his metal rods to a local businessman. Mr. Octopus has many rods whose length are represented by array - lengths = {lengths[0], lengths[1], lengths[2],...}.The local businessman will only pay for rods that have same length. Let's say Mr. Octopus plans to sell rods of lengthL only. Then he had to cut each rod 0 or more time, so that he can maximize the profit. The remaining rods whose length is not L will be thrown away. Price of N rods of length L will be N × L × unit_price. Also note that for each cut made to a rod, he had to pay cut_cost.
What is the maximum amount of money Mr. Octopus can make? You have to complete the function int maxProfit(int cut_cost, int unit_price, int[] lengths).
My Solution Stratagy:
The problem states we have to find solution in such a way that seller must get maximum profit.you can do this by cutting irons with minimum length(I assume that if we take a minimum length iron ,we can cut lot number of pieces ,so that seller profit become high)
pseudo code :
1.get the minimum length iron from given array of items
2.cut each and every iron with respect to minimum length iron
3.thrown away if some of irons length not compatable with minimum iron.
Program Solution:
ShopSelling.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package craeercu.com;
/**
*
* @author shashi
*/
public class ShopSelling {
//the person gives iron ores ,cuting cost,unit cost return total cosst
public int totalcost(int unitcost,int cuttngcost,int[]irons)
{
//to get max profit select a minimum length iron from given itorns
//get the min length iron
int shortiron=miniron(irons);
//get each and every irin from the array of irons and match with shortiron
//if it is equal or atleast great
int numberofirons=0;
int noofcuttings=0;
int currentiron;
int aftercutting;
for(int i=0;i<irons.length;i++)
{
//get the first one
currentiron=irons[i];
//check for x length ie short
if(currentiron>shortiron)
{
//cut this till you find some exact length
while(currentiron>=shortiron)
{
aftercutting=currentiron-shortiron;
noofcuttings++;
numberofirons++;
currentiron=aftercutting;
}
}
if(currentiron==shortiron)
{
//for same length irons
numberofirons++;
}
}
//calclate final result
int result=(numberofirons*unitcost*shortiron)-(noofcuttings*cuttngcost);
return result;
}
public int miniron(int []irons)
{
int min=irons[0];
for(int i=1;i<irons.length;i++)
{
if(irons[i]<min)
min=irons[i];
}
return min;
}
}
MainClass
/**
*
* @author shashi
*/
public class CraeercuCom {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ShopSelling sell=new ShopSelling();
int []irons={10,20,30,40};
int cost=sell.totalcost(10, 1, irons);
System.out.print(cost);
}
}
Mr. Xhas recently shut down his factory and wants to sell off his metal rods to a local businessman. Mr. Octopus has many rods whose length are represented by array - lengths = {lengths[0], lengths[1], lengths[2],...}.The local businessman will only pay for rods that have same length. Let's say Mr. Octopus plans to sell rods of lengthL only. Then he had to cut each rod 0 or more time, so that he can maximize the profit. The remaining rods whose length is not L will be thrown away. Price of N rods of length L will be N × L × unit_price. Also note that for each cut made to a rod, he had to pay cut_cost.
What is the maximum amount of money Mr. Octopus can make? You have to complete the function int maxProfit(int cut_cost, int unit_price, int[] lengths).
My Solution Stratagy:
The problem states we have to find solution in such a way that seller must get maximum profit.you can do this by cutting irons with minimum length(I assume that if we take a minimum length iron ,we can cut lot number of pieces ,so that seller profit become high)
pseudo code :
1.get the minimum length iron from given array of items
2.cut each and every iron with respect to minimum length iron
3.thrown away if some of irons length not compatable with minimum iron.
Program Solution:
ShopSelling.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package craeercu.com;
/**
*
* @author shashi
*/
public class ShopSelling {
//the person gives iron ores ,cuting cost,unit cost return total cosst
public int totalcost(int unitcost,int cuttngcost,int[]irons)
{
//to get max profit select a minimum length iron from given itorns
//get the min length iron
int shortiron=miniron(irons);
//get each and every irin from the array of irons and match with shortiron
//if it is equal or atleast great
int numberofirons=0;
int noofcuttings=0;
int currentiron;
int aftercutting;
for(int i=0;i<irons.length;i++)
{
//get the first one
currentiron=irons[i];
//check for x length ie short
if(currentiron>shortiron)
{
//cut this till you find some exact length
while(currentiron>=shortiron)
{
aftercutting=currentiron-shortiron;
noofcuttings++;
numberofirons++;
currentiron=aftercutting;
}
}
if(currentiron==shortiron)
{
//for same length irons
numberofirons++;
}
}
//calclate final result
int result=(numberofirons*unitcost*shortiron)-(noofcuttings*cuttngcost);
return result;
}
public int miniron(int []irons)
{
int min=irons[0];
for(int i=1;i<irons.length;i++)
{
if(irons[i]<min)
min=irons[i];
}
return min;
}
}
MainClass
/**
*
* @author shashi
*/
public class CraeercuCom {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ShopSelling sell=new ShopSelling();
int []irons={10,20,30,40};
int cost=sell.totalcost(10, 1, irons);
System.out.print(cost);
}
}
Không có nhận xét nào:
Đăng nhận xét