poj 1017 Packets

题意:

有一些货物需要装在包裹里,这些包裹是正方形的,并且高度相同,规模分别有1 * 1,2 * 2,3 * 3,4 * 4,5 * 5,6 * 6。

现在给出每种包裹需要的数量,问最少需要多少个包裹可以装下这些货物。

思路:

需要最少的包裹,那肯定用最大的包裹装,即6 * 6。

首先装6 * 6的,然后装5 * 5的,5 * 5的话剩下11个1 * 1;

再装4 * 4,4 * 4的会剩下5个2 * 2;

再考虑3 * 3,这个的情况就比较复杂,当3 * 3 的包裹数量除以4余数是1时,剩下3个3 * 3,可以放5个2 * 2,7个1 * 1;为2时,放3个2 * 2,6个1 * 1;为3时,放1个2 * 2,5个1 * 1;

再考虑2 * 2和1 * 1,就比较好考虑了。

以上结果都是画图可得并且贪心取得最大(3 * 3的画法不止一种,结果不同)。

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int a[10];
 5 
 6 int main()
 7 {
 8     while (1)
 9     {
10         int sum = 0;
11         
12         for (int i = 1;i <= 6;i++)
13         {
14             scanf("%d",&a[i]);
15             
16             sum += a[i];
17         }
18         
19         if (sum == 0) break;
20         
21         int ans = 0;
22         
23         ans += a[6];
24         
25         ans += a[5];
26         
27         int res1 = 11 * a[5];
28         
29         ans += a[4];
30         
31         int res2 = 5 * a[4];
32         
33         ans += a[3] / 4;
34         
35         if (a[3] % 4)
36         {
37             ans++;
38             
39             if (a[3] % 4 == 1)
40             {
41                 res2 += 5;
42                 res1 += 7;
43             }
44             
45             if (a[3] % 4 == 2)
46             {
47                 res2 += 3;
48                 res1 += 6;
49             }
50             
51             if (a[3] % 4 == 3)
52             {
53                 res2 += 1;
54                 res1 += 5;
55             }
56         }
57         
58         if (a[2] > res2)
59         {
60             a[2] -= res2;
61             
62             ans += a[2] / 9;
63             
64             if (a[2] % 9)
65             {
66                 ans++;
67                 
68                 res1 += 36 - (a[2] % 9 * 4);
69             }
70         }
71         else
72         {
73             res2 -= a[2];
74             
75             res1 += res2 * 4;
76         }
77         
78         a[1] -= res1;
79         
80         if (a[1] > 0)
81         {
82             ans += a[1] / 36;
83             
84             if (a[1] % 36) ans++;
85         }
86         
87         printf("%d\n",ans);
88     }
89     
90     return 0;
91 }

 

posted @ 2018-04-12 15:36  qrfkickit  阅读(132)  评论(0编辑  收藏  举报