编程介的小学生 2019-03-25 17:13 采纳率: 20.5%
浏览 285

一个圆形的管道的堆放的算法问题,怎么用C语言的办法解决呢

Problem Description
Filters, or programs that pass processed'' data through in some changed form, are an important class of programs in the UNIX operating system. A pipe is an operating system concept that permits data toflow'' between processes (and allows filters to be chained together easily.)

This problem involves maximizing the number of pipes that can be fit into a storage container (but it's a pipe fitting problem, not a bin packing problem).

A company manufactures pipes of uniform diameter. All pipes are stored in rectangular storage containers, but the containers come in several different sizes. Pipes are stored in rows within a container so that there is no space between pipes in any row (there may be some space at the end of a row), i.e., all pipes in a row are tangent, or touch. Within a rectangular cross-section, pipes are stored in either a grid pattern or a skew pattern as shown below: the two left-most cross-sections are in a grid pattern, the two right-most cross-sections are in a skew pattern.

Note that although it may not be apparent from the diagram, there is no space between adjacent pipes in any row. The pipes in any row are tangent to (touch) the pipes in the row below (or rest on the bottom of the container). When pipes are packed into a container, there may be ``left-over'' space in which a pipe cannot be packed. Such left-over space is packed with padding so that the pipes cannot settle during shipping.

Input
The input is a sequence of cross-section dimensions of storage containers. Each cross-section is given as two real values on one line separated by white space. The dimensions are expressed in units of pipe diameters. All dimensions will be less than 2^7. Note that a cross section with dimensions a*b can also be viewed as a cross section with dimensions b*a.

Output
For each cross-section in the input, your program should print the maximum number of pipes that can be packed into that cross section. The number of pipes is an integer -- no fractional pipes can be packed. The maximum number is followed by the word grid'' if a grid pattern results in the maximal number of pipes or the wordskew'' if a skew pattern results in the maximal number of pipes. If the pattern doesn't matter, that is the same number of pipes can be packed with either a grid or skew pattern, then the word ``grid'' should be printed.

Sample Input
3 3
2.9 10
2.9 10.5
11 11

Sample Output
9 grid
29 skew
30 skew
126 skew

  • 写回答

1条回答 默认 最新

  • a81836620 2019-03-25 17:36
    关注
    #include <stdio.h>
    #include<math.h>
    //计算交错排列法的数量
    int Skew(float x, float y) {
        //fSqrt3_2为开方3除以2,表示相邻两行顶部间的距离
        static const float fSqrt3_2 = 0.8660254f;
        //计算可以排下的总行数。除最底行高为1,其余行高为fSqrt3_2
        int nRows = (y >= 1) + (int)((y - 1) / fSqrt3_2);
        //先计算出最底一行排满的列数,如果最底行剩下的空间不足0.5
        //则说明奇数行(底行为0)的列数比偶数行少1个,要对奇数行每行减1
        return (nRows * (int)x - (nRows / 2) * (x - (int)x < 0.5f));
    }
    //主函数
    int main(void) {
        //循环读入并处理所有数据
        float x, y;
        while(scanf("%f %f",&x,&y)!=EOF){
            //网格方式排列,即为简单的行列取整。交错方式要计算两个方向
            int nGrid = (int)x * (int)y;
            int nSkew = fmax(Skew(x, y), Skew(y, x));
            //输出最大的管子数及其排列方式
            printf("%d%s",(int)fmax(nGrid, nSkew),(nGrid >= nSkew ? " grid" : " skew"));
            printf("\n");
        }
        return 0;
    }
    
    
    
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面