zoukankan      html  css  js  c++  java
  • POJ1017 Packets

    题目来源:http://poj.org/problem?id=1017

    题目大意:

      某工厂有不同型号的货物要装箱。所有货物的高度都为h,底面大小分别为1*1,2*2,3*3,4*4,5*5,6*6.这些货物都要被装进高为h,底面6*6的盒子中。工厂希望用最少数目的箱子打包所有的货物。

    输入:由多组数据组成,每行一组。各数分别表示1*1,2*2,3*3,4*4,5*5,6*6货物的个数。6个0代表结束。

    输出:每行对应一个输入,输出一个整数,表示需要的最少的箱子数。


    Sample Input

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

    Sample Output

    2 
    1

    模拟水过,见代码。

      1 //////////////////////////////////////////////////////////////////////////
      2 //        POJ1017 Packets
      3 //        Memory: 248K        Time: 47MS
      4 //        Language: C++        Result: Accepted
      5 //////////////////////////////////////////////////////////////////////////
      6 
      7 #include <iostream>
      8 
      9 using namespace std;
     10 
     11 int main() {
     12     int box[6];
     13     int count;
     14     while (true) {
     15         cin >> box[0] >> box[1] >> box[2] >> box[3] >> box[4] >> box[5];
     16         if ((box[0] | box[1] | box[2] | box[3] | box[4] | box[5]) == 0) {
     17             break;
     18         }
     19         count = 0;
     20 
     21         while (box[5] != 0) {
     22             ++count;
     23             --box[5];
     24         }
     25         while (box[4] != 0) {
     26             ++count;
     27             --box[4];
     28             for (int i = 0; i < 11; ++i) {
     29                 if (box[0] != 0) {
     30                     --box[0];
     31                 } else {
     32                     break;
     33                 }
     34             }
     35         }
     36         while (box[3] != 0) {
     37             int area = 36 - 16;
     38             ++count;
     39             --box[3];
     40             for (int i = 0; i < 5; ++i) {
     41                 if (box[1] != 0) {
     42                     --box[1];
     43                     area -= 4;
     44                 } else {
     45                     break;
     46                 }
     47             }
     48             for (int i = 0; i < area; ++i) {
     49                 if (box[0] != 0) {
     50                     --box[0];
     51                 } else {
     52                     break;
     53                 }
     54             }
     55         }
     56         while (box[2] != 0) {
     57             int area = 36;
     58             ++count;
     59             for (int i = 0; i < 4; ++i) {
     60                 if (box[2] != 0) {
     61                     area -= 9;
     62                     --box[2];
     63                 } else {
     64                     break;
     65                 }
     66             }
     67             if (area == 0) {
     68                 continue;
     69             } else if (area == 27) {
     70                 for (int i = 0; i < 5; ++i) {
     71                     if (box[1] != 0) {
     72                         --box[1];
     73                         area -= 4;
     74                     } else {
     75                         break;
     76                     }
     77                 }
     78             } else if (area == 18) {
     79                 for (int i = 0; i < 3; ++i) {
     80                     if (box[1] != 0) {
     81                         --box[1];
     82                         area -= 4;
     83                     } else {
     84                         break;
     85                     }
     86                 }
     87             } else if (area == 9) {
     88                 if (box[1] != 0) {
     89                     --box[1];
     90                     area -= 4;
     91                 }
     92             } 
     93             for (int i = 0; i < area; ++i) {
     94                 if (box[0] != 0) {
     95                     --box[0];
     96                 } else {
     97                     break;
     98                 }
     99             }
    100         }
    101         while (box[1] != 0) {
    102             ++count; 
    103             int area = 36;
    104             for (int i = 0; i < 9; ++i) {
    105                 if (box[1] != 0) {
    106                     --box[1];
    107                     area -= 4;
    108                 } else {
    109                     break;            
    110                 }
    111             }
    112             for (int i = 0; i < area; ++i) {
    113                 if (box[0] != 0) {
    114                     --box[0];
    115                 } else {
    116                     break;
    117                 }
    118             }
    119         }
    120         while (box[0] != 0) {
    121             ++count;
    122             for (int i = 0; i < 36; ++i) {
    123                 if (box[0] != 0) {
    124                     --box[0];
    125                 } else {
    126                     break;
    127                 }
    128             }
    129         }
    130         cout << count << endl;
    131     }
    132     return 0;
    133 }
    View Code

    Discuss里有牛人的精辟代码,以上拙码瞬间相形见绌啊。

     1 #include<stdio.h>
     2 int main()
     3 {
     4     int n,a,b,c,d,e,f,x,y;
     5     int u[4]={0,5,3,1};
     6     while(1)
     7     {
     8         scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
     9         if(a==0&&b==0&&c==0&&d==0&&e==0&&f==0)
    10             break;
    11         n=d+e+f+(c+3)/4;//懂了
    12         y=5*d+u[c%4];//在已有n个的情况下,能装下y个2*2的
    13         if(b>y)
    14             n+=(b-y+8)/9;//把多的2*2的弄进来
    15         x=36*n-36*f-25*e-16*d-9*c-4*b;
    16         if(a>x)
    17             n+=(a-x+35)/36;//把1*1的弄进来
    18         printf("%d
    ",n);
    19     }
    20     return 0;
    21 }
    View Code
  • 相关阅读:
    SQL Azure (17) SQL Azure V12
    Microsoft Azure News(5) Azure新DV2系列虚拟机上线
    Azure Redis Cache (3) 在Windows 环境下使用Redis Benchmark
    Azure PowerShell (11) 使用自定义虚拟机镜像模板,创建Azure虚拟机并绑定公网IP(VIP)和内网IP(DIP)
    Windows Azure Virtual Machine (31) 迁移Azure虚拟机
    Windows Azure Web Site (16) Azure Web Site HTTPS
    Azure China (12) 域名备案问题
    一分钟快速入门openstack
    管理员必备的Linux系统监控工具
    Keepalived+Nginx实现高可用和双主节点负载均衡
  • 原文地址:https://www.cnblogs.com/dengeven/p/3227836.html
Copyright © 2011-2022 走看看