昨晚没写完,今天还是补上来,不知道正确率是多少了,自己运行是可以的。
有什么错误欢迎大家留言相告,互相学习。
对于1*1、2*2、3*3、4*4、5*5、6*6大小的产品,装在6*6的包裹里,求得最少包裹数。
输入:
0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0
输出:
2
1
装包裹的问题最优化解题思路:
最考虑体积最大的产品,再整体考虑剩余空间装小体积产品~
import java.util.ArrayList;
import java.util.Scanner;
public class Sohub {
/**
* @param args
*/
/*解题思路:
* 先装所有大体积产品:6*6、5*5、4*4、3*3
* 此时包裹至少a[5]+a[4]+a[3]+(a[2]+3)/4个
* 装3*3产品的包裹未装满的情况:
* 只有一个3*3时,还可装2*2的产品5个,同理,数组设为u[]=[0,5,3,1];
* 余下只考虑剩余空间与2*2、1*1的关系即完成。
* */
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int count = 0;
int a[] = new int[6];
ArrayList<Integer> results= new ArrayList<Integer>();
while(true){
for(int i=0;i<6;i++){
a[i] = in.nextInt();
if(a[i] == 0)count++;
}
if(count == 6)break;
int x=SolveProblem(a);
results.add(x);
count = 0;
}
for(int i=0;i<results.size();i++){
System.out.println(results.get(i));
}
}
public static int SolveProblem(int a[]){
int t[]={0,5,3,1};//这是3*3的箱子剩余自己装的时候
int total=0,x,y,n;
total = a[5]+a[4]+a[3]+(a[2]+3)/4;
x = t[a[2]%4]+5*a[3];//剩余空间可装2*2的个数
//剩余空间不够装2*2时,需要另加包裹
if(a[1] > x){
total += (a[1]-x+8)/9;
}
//此时包裹剩余空间
n=36*total-36*a[5]-25*a[4]-16*a[3]-9*a[2]-4*a[1];
if(a[0]>n){
total+=(a[0]-n+35)/36;
}
/*for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}*/
return total;
}
}