Description
A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6*6. Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a program.
Input
The input file consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated by one space representing successively the number of packets of individual size from the smallest size 1*1 to the biggest size 6*6. The end of the input file is indicated by the line containing six zeros.
Output
The output file contains one line for each line in the input file. This line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. There is no line in the output file corresponding to the last “null” line of the input file.
Sample Input
0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0
Sample Output
2
1
这是一道贪心算法,思想很简单,每次从最大的先放,依次放入,2*2包裹和1*1包裹是用来填补空位的。
6*6:一个包裹最多只能放入1个;
5*5:一个包裹最多只能放入1个、剩余的只能用1*1来填充;
4*4:一个包裹最多只能放入1个,剩余可用5个2*2填充,不足5个用1*1填充;
3*3:一个包裹最多只能放入4个,不满四个用2*2和1*1填充(这里需要判断3*3包裹个数,个数不同余下的空位可用2*2填充的个数也不同);
2*2:判断是否比可利用空间的个数多,多则需要增加包裹数量;
1*1:同2*2;
#include<iostream>
#include<vector>
using namespace std;
//放入3*3包裹后,剩余的空间可放2*2包裹的数量
int remainder3_3(int n){
int n1 = 0;//可以放2*2包裹的数量
if (n == 3){
n1 = 1;
}
else if (n == 2){
n1 = 3;
}
else if (n == 1){
n1 = 5;
}
return n1;
}
int main(){
int n;//最少需要包裹数量
int input[6];
vector<int> output;
//输入
while (true)
{
int n51 = 0;
int n42 = 0;
int n32 = 0;
int n31 = 0;
int n21 = 0;
int n1 = 0;//
for (int i = 0; i < 6; i++)
{
cin >> input[i];
}
//若全为0则退出
if (input[0] == 0 && input[1] == 0 && input[2] == 0
&& input[3] == 0 && input[4] == 0 && input[5] == 0)
break;
//处理
n = input[5];//一个包裹最多只能放入1个6*6包裹
n += input[4];//一个包裹最多只能放入1个5*5包裹
n51 = input[4] * 11;//装入5*5后包裹空余的大小,全部只能放1*1
n += input[3];//一个包裹最多只能放入1个4*4包裹
n42 = input[3] * 5;//装入4*4后包裹空余的大小能装入2*2包裹的个数
n += input[2] / 4;//一个包裹最多只能放入4个3*3包裹
if (input[2]%4 != 0){
n++;
n32 = remainder3_3(input[2]%4);//装入3*3后包裹空余的大小能装入2*2包裹的个数
n31 = 36 - input[2] % 4 * 9 - n32 * 4;//假如能放入3*3和2*2的都放至最多后,剩余可放1*1的数量
}
if (input[1] > n42 + n32){
input[1] -= (n42 + n32);//装入空余地方后剩下的2*2包裹个数
n += input[1] / 9;//一个包裹最多只能放入9个2*2包裹
if (input[1] % 9 != 0){
n++;
n21 = 36 - input[1] % 9 * 4;//装入2*2后包裹空余的大小能装入1*1包裹的个数
}
}
else{
n1 = (n42 + n32 - input[1]) * 4;//将所有2*2包裹填入后,原本可以装入2*2包裹的空间可装1*1包裹的个数
}
n1 = n51 + n31 + n21 + n1;//最终剩余空间大小,只剩1*1包裹
if (input[0] > n1){
input[0] -= n1;
n += input[0] / 36;
if (input[0] % 36 != 0){
n++;
}
}
output.push_back(n);
}
for (int i = 0; i < output.size(); i++){
cout << output[i] << endl;
}
output.clear();
return 0;
}
到网上看 一下别人写的,思路都差不多,不过有些人的代码更简洁些,但是代码行数也差不了几行,这里就不贴出来。