一道简单的题,我就是作不对,为什么?

dawnhorizon 2003-05-01 06:11:35
--------------------------------------------------------------------------------

Packets

--------------------------------------------------------------------------------

Time limit: 1 Seconds Memory limit: 32768K
Total Submit: 358 Accepted Submit: 104

--------------------------------------------------------------------------------
A factory produces products packed in square packets of the same height h and of the sizes 1x1, 2x2, 3x3, 4x4, 5x5, 6x6. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6x6. 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 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 to the biggest size . The end of the input is indicated by the line containing six zeros.


Output

The output contains one line for each line in the input. 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 corresponding to the last ``null'' line of the input.


Sample Input

0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0


Sample Output

2
1


我的代码是:
#include <iostream.h>

int main()
{
int a[6];
int total=0;
cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5];
while ((a[0]!=0)||(a[1]!=0)||(a[2]!=0)||(a[3]!=0)||(a[4]!=0)||(a[5]!=0))
{
total=0;
if (a[5]!=0)
{
total+=a[5];
}
if (a[4]!=0)
{
total+=a[4];
if (a[0]>11*a[4])
{
a[0]=a[0]-11*a[4];
}else
{
a[0]=0;
}
}
if (a[3]!=0)
{
total+=a[3];
if (a[1]>5*a[3])
{
a[1]=a[1]-5*a[3];
}else if (a[0]>5*a[3]-a[1])
{
a[0]=a[0]-20*a[3]+4*a[1];
a[1]=0;

}else
{
a[1]=0;
a[0]=0;
}
}
if (a[2]!=0)
{

if (a[2]%4==0)
{
total+=a[2]/4;
}else
{
total=total+a[2]/4+1;
int temp=(3-a[2]%4)*2+1;

if (a[1]>temp)
{
a[1]-=temp;
if (a[0]>temp*5)
{
a[0]=a[0]-temp*5;
}else
{
a[0]=0;
}
}else
{
if (a[0]>36-a[1]*4-(a[2]%4)*9)
{
a[0]=a[0]-36+a[1]*4+(a[2]%4)*9;
}else
{
a[0]=0;
}
a[1]=0;
}

}

}
if (a[1]!=0)
{
if (a[1]%9==0)
{
total+=a[1]/9;
}else
{
total=total+a[1]/9+1;
if (36-4*(a[1]%9)<a[0])
{
a[0]=a[0]-36+4*(a[1]%9);
}else
{
a[0]=0;
}
}
}
if (a[0]!=0)
{
total++;
total+=a[0]/36;
}
cout<<total<<endl;
cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5];
}
return 0;
}
可是无限Wrong,高手给看看?
(http://acm.zju.edu.cn/show_problem.php?pid=1307)
...全文
28 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lymgf 2003-05-02
  • 打赏
  • 举报
回复
搂主,错误如下:
if (a[0]!=0)
{
total++; //不能直接加1
total+=a[0]/36;
}

改为:
if (a[0]!=0)
{
total+=a[0]/36;
if (a[0]%36) total++;
}

修改后Accepted.
ZhangYv 2003-05-02
  • 打赏
  • 举报
回复
楼主测测这个程序吧,偶又错了,有没好的测试数据?
//装箱问题,利用数据的特殊性直接模拟求解
#include <iostream.h>
int main()
{
int a[6];
long s = 0,t,p;
cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5];
while (a[0] || a[1] || a[2] || a[3] || a[4] || a[5]){
s = a[5];
a[5] = 0;

s += a[4];
t = 11*a[4];
a[4] = 0;
a[0] = (a[0]-t>0)?a[0]-t:0;

s += a[3];
p = a[1];
a[1] = (a[1] - 5*a[3]>0)?a[1] - 5*a[3]:0;
t = 20 * a[3] - 4 * (p-a[1]);
a[0] = (a[0]-t>0)?a[0]-t:0;
if (a[2] != 0){
s += a[2]/4 + ((a[2]%4)?1:0);
a[2] %= 4;
if (a[2] != 0){
switch (a[2]){
case 3: t = 1;break;
case 2: t = 3;break;
case 1: t = 5;break;
}
p = a[1];
a[1] = (a[1] - t>0)?a[1] - t:0;
t = (4-a[2])*9 - (p-a[1]) * 4;
a[0] = (a[0]-t>0)?a[0]-t:0;
}
}
if (a[1] != 0){
s += a[1]/9 + ((a[1]%9)?1:0);
a[1] %= 9;
if (a[1] != 0){
t = (9-a[1])*2;
a[0] = (a[0]-t>0)?a[0]-t:0;
}
}
if (a[0] != 0)
s += a[0]/36 + ((a[0]%36)?1:0);
cout << s << endl;
cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5];
}
return 1;
}
dawnhorizon 2003-05-02
  • 打赏
  • 举报
回复
lymgf(自封五星★★★★★) 太棒了,我没有想到能整除36的情况,perfect
ZhangYv 2003-05-01
  • 打赏
  • 举报
回复
这是刚刚wrong answer的程序,改动了一点点:
#include <iostream.h>
int main()
{
int a[6];
long s = 0,t,p;
cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5];
while (a[0] || a[1] || a[2] || a[3] || a[4] || a[5]){
s = a[5];
a[5] = 0;

s += a[4];
t = 11*a[4];
a[4] = 0;
a[0] = (a[0]-t>0)?a[0]-t:0;

s += a[3];
p = a[1];
a[1] = (a[1] - 5*a[3]>0)?a[1] - 5*a[3]:0;
t = 20 * a[3] - 4 * p;
a[0] = (a[0]-t>0)?a[0]-t:0;

if (a[2] != 0){
s += a[2]/4 + (a[2]%4)?1:0;
a[2] %= 4;
if (a[2] != 0){
switch (a[2]){
case 3: t = 1;break;
case 2: t = 3;break;
case 1: t = 5;break;
}
p = a[1];
a[1] = (a[1] - t>0)?a[1] - t:0;
t = (4-a[2])*9 - p * 4;
a[0] = (a[0]-t>0)?a[0]-t:0;
}
}
if (a[1] != 0){
s += a[1]/9 + (a[1]%9)?1:0;
a[1] %= 9;
if (a[1] != 0){
t = (9-a[1])*2;
a[0] = (a[0]-t>0)?a[0]-t:0;
}
}
if (a[0] != 0)
s += a[0]/36 + (a[0]%36)?1:0;
cout << s << endl;
cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5];
}
return 1;
}
ZhangYv 2003-05-01
  • 打赏
  • 举报
回复
OOOOooooo.........又挂了一次,受不了了!
ZhangYv 2003-05-01
  • 打赏
  • 举报
回复
啊,刚才没看楼主的解法,我的想法和你的一样,挂了3次...贴出来看看
//装箱问题,利用数据的特殊性直接模拟求解
#include <iostream.h>
int main()
{
int a[6];
long s = 0,t,p;
cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5];
while (a[0] || a[1] || a[2] || a[3] || a[4] || a[5]){
s = a[5];
a[5] = 0;

s += a[4];
t = 11*a[4];
a[4] = 0;
a[0] = (a[0]-t>0)?a[0]-t:0;

s += a[3];
p = a[1];
a[1] = (a[1] - 5*a[3]>0)?a[1] - 5*a[3]:0;
if (a[1] == 0){
t = 20 * a[3] - 4 * p;
a[0] = (a[0]-t>0)?a[0]-t:0;
}

if (a[2] != 0){
s += a[2]/4 + (a[2]%4)?1:0;
a[2] %= 4;
if (a[2] != 0){
switch (a[2]){
case 3: t = 1;break;
case 2: t = 3;break;
case 1: t = 5;break;
}
p = a[1];
a[1] = (a[1] - t>0)?a[1] - t:0;
if (a[1] == 0){
t = (4-a[2])*9 - p * 4;
a[0] = (a[0]-t>0)?a[0]-t:0;
}
}
}
if (a[1] != 0){
s += a[1]/9 + (a[1]%9)?1:0;
a[1] %= 9;
if (a[1] != 0){
t = (9-a[1])*2;
a[0] = (a[0]-t>0)?a[0]-t:0;
}
}
if (a[0] != 0)
s += a[0]/36 + (a[0]%36)?1:0;
cout << s << endl;
cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5];
}
return 1;
}
ZhangYv 2003-05-01
  • 打赏
  • 举报
回复
刚才这道题我也挂了,不知道为什么...
alidiedie 2003-05-01
  • 打赏
  • 举报
回复
我觉得你的思路应该可以解决的,但越往后结构越复杂也越容易出错。觉得疑惑的地方是:从大到小的顺序考虑是否能保证所需的packet最少?直觉上是对的,没证明。

关注

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧