pku 1017 Packets
来源:互联网 发布:517网络加速器官网 编辑:程序博客网 时间:2024/05/16 17:41
Packets
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 26775Accepted: 8858
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
解题报告:
算法是贪心,用6*6的箱子先装6*6物品;再装5*5的物品,空隙装11个
1*1;再装4*4的物品,空隙装2*2(无需考虑装1*1,因为2*2接下来可
用四个1*1代替);再装3*3,此时要考虑最后一个装3*3的箱子中3*3
物品的个数,若1个3*3,则5个2*2,7个1*1,若2个3*3,则3个2*2,6
个1*1,若3个3*3,则1个2*2,5个1*1;
自己的代码:
#include<iostream>
using namespace std;
int main()
{
int num,a[7],q;
while(cin>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]&&a[1]+a[2]+a[3]+a[4]+a[5]+a[6]!=0)
{
num=a[6]+a[5]+a[4];
a[1]-=11*a[5]; /*此处忘记乘11贡献检查了n次*/
a[2]-=5*a[4];
num+=(a[3]+3)/4;
q=a[3]%4;
switch(q)
{
case 1:
a[2]-=5;
a[1]-=7;
break;
case 2:
a[2]-=3;
a[1]-=6;
break;
case 3:
a[2]-=1;
a[1]-=5;
break;
default:
break;
}
if(a[2]<0)
{
a[1]=a[1]+4*a[2];
}
else
{
num+=(a[2]+8)/9;
q=a[2]%9;
if(q>0) /*此处忘记判断条件导致wrong一次,没考虑q=0时*/
a[1]=a[1]-(36-4*q);
}
if(a[1]>0)
{
num+=(a[1]+35)/36;
}
cout<<num<<endl;
}
return 0;
}/*代码质量太差*/
优秀的代码:
#include <iostream>using namespace std;int main(){int a[10],i,j,sum,m,left1,left2;int u[4]={0,5,3,1};while (1){sum=0;for(i=1;i<=6;i++){cin>>a[i];sum+=a[i];}if(sum==0)break;m=a[6]+a[5]+a[4]+(3+a[3])/4;left2=a[4]*5+u[a[3]%4];if(a[2]>left2)m+=(a[2]-left2+8)/9;left1=m*36-a[6]*36-a[5]*25-a[4]*16-a[3]*9-a[2]*4;if(a[1]>left1)m+=(a[1]-left1+35)/36;cout<<m<<endl;}return 0;}
总结:wrong 两次都因为程序错误,考虑不周;以后要仔细考虑清楚每一步,
回过头来检查会浪费很多时间。正确之后尝试优化代码和算法。
- pku 1017 Packets
- pku 1017 Packets
- pku 1017 Packets
- PKU ACM 1017 Packets
- PKU 1017 Packets
- PKU POJ 1017 Packets 产品包装问题
- 1017 Packets
- poj 1017 Packets【贪心】
- POJ 1017 Packets(贪心)
- poj 1017 Packets
- POJ 1017 Packets
- POJ 1017 Packets
- poj 1017 Packets(贪心)
- poj 1017 Packets
- poj 1017 Packets
- POJ 1017 Packets
- POJ 1017 Packets
- POJ 1017 Packets
- Ubuntu 连接 Windows XP
- 跟我从头学TAO编程系列 -1
- 在Workflow中使用Siebel Operation
- SOA ~
- http消息头解读
- pku 1017 Packets
- 01背包问题
- mysql 表名大写
- 使用Wake On Lan远程唤醒
- GBK与UTF8 编码转化(C++)
- Asterisk的SIP type和身份认证
- 无向图深度优先遍历和广度优先遍历
- C++ primer 顺序容器
- include file与include page的区别