Title Source: http://bailian.openjudge.cn/practice/1017/
1017: Boxing Problem
-----------------------------------------------------
Total time limit: 1000ms memory Limit: 65536kB describes a factory-manufactured product shapes are cuboid, their height is h, length and width are equal, a total of six models, their length and width of 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are usually wrapped in a 6*6*h box and mailed to the customer. Because the postage is very expensive, the factory has to find ways to reduce the number of packages each time the order is shipped. They need a good program to help them solve the problem and save money. Now this program is designed by you. The input input file consists of several lines, each of which represents an order. The line in each order consists of six integers, separated by a space, which is the number of six products from 1*1 to 6*6 respectively. The input file ends with a line consisting of 6 0. Output in addition to the last line of input 6 0, each row in the input file corresponds to the output file line, each line output an integer representing the corresponding order of the minimum number of packages required. Sample input
0 0 4 0 0 1
7 5 1 0 0 0
Sample output
2
-----------------------------------------------------
Thinking of solving problems
Greedy algorithm, always first put large volume parts
6
5,1
4,2,1//note. Easy error
3,2,1
2,1
1
-----------------------------------------------------
Code
#include <iostream> #include <vector> using namespace std;
int Pack (int *order); int main () { int order[6]; &NBSP ; //a piece of input order vector<int> package; & nbsp //vector of output # of packages for each order vector< Int>::iterator iter; //vector iterator INT thepack; - //
# of packages for each order int i=0; /* Read the First order line */ for (i=0; i<6; i++) { &N Bsp
CIN >> Order[i]; } /* Main loop */ while (! Order[0]==0 &Amp;& order[1]==0 && order[2]==0 && order[3]==0 && order[4]==0 && order[5]==0))
{ Thepack = Pack (order), Package.push_back (thepack); //read in a new order line for (i=0; i<6; i++) { CIN >> Order[i] } &nbs P } /* Print result */ for (Iter=package.begin (); Iter!=package.end (); iter++) { cout << *iter << Endl, } Retur
n 0; } int Pack (int *order) { int j=0, result=0; int num1 = order[0]; //# of 1*1 int num2 = order[1]; &NBSp /# of 2*2 int num3 = order[2]%4; &NBS P //remaining 3*3 int num22 = 0; //remaining remaining 2*2 /pack 6*6 result + Ord
ER[5];
/pack 5*5 result + = order[4]; if (num1>order[4]*11) { NUM1-= order[4]*11; } &NB Sp Else { NUM1 = 0, } //pack 4*4
Result + = Order[3]; if (num2 > Order[3]*5) { num2-= order[3]*5; } &NB Sp Else { /*----------------Attention (BEGIN)-----------------*/ if (num1>4* (order[3]*5-NUM2) { NUM1-= (order[3]*5-num2); &NBSP ; else { N
UM1 = 0;
} num2 = 0; /*----------------Attention (end) -----------------*/ } /
/pack 3*3 result + = ORDER[2]/4;
switch (num3) { case 0:break, Case 1: { if (num2>5) & nbsp { NUM2-=
5; if (Num1 > 7) & nbsp { NUM1-= 7; &nbs P } & nbsp Else { &NB Sp
NUM1 = 0; } & nbsp } else {& nbsp if (num1> (27-4*num2)) { N
UM1-= 27-4*num2; } other &NB Sp { & nbsp
NUM1 = 0; } & nbsp
num2 = 0; result +
+;
break; } Case 2: { if (num2>3) / nbsp { NUM2-= 3 &NBS P if (Num1 > 6) {
NUM1-= 6; } & nbsp Else { &NB Sp
NUM1 = 0; } & nbsp } else {& nbsp if (Num1 > (18-num2*4)) &NBSP ; { &NBSp
NUM1-= 18-num2*4; } & nbsp Else { &NB Sp
NUM1 = 0; } & nbsp
num2 = 0; result +
+;
break; } Case 3: { if (num2>1) / nbsp { NUM2-= 1; if (Num1 > 5) & nbsp { / NUM1-=
5; } & nbsp Else { &NB Sp
NUM1 = 0; } & nbsp } else {& nbsp if (Num1 > (9-num2*4)) { &NBsp
NUM1-= 9-num2*4; } & nbsp Else { &NB Sp
NUM1 = 0; } & nbsp
num2 = 0; result +
+;
break;
} Default:break;
} //pack remaining 2*2 result + = NUM2/9;
NUM22 = num2%9; if (Num22 > 0) {
result + +; if (Num1 > (36-4*NUM22)) { &N Bsp
NUM1-= 36-4*NUM22; } else { & nbsp
NUM1 = 0;
} } //pack remaining 1*1 result + = NUM1/36; if (num1%36>0) { result++, } return
Result
}