如何在C中相互打印多个盒子

时间:2018-03-21 09:31:01

标签: c

我目前正在攻读计算机科学课程,并且发现很难完成挑战问题!它意味着读取一个数字,然后使用字符#with Spaces填充两者之间的间隙,在彼此内部绘制多个方框:例如:

多少个盒子:3个

#######
#     #
# ### #
# # # #
# ### #
#     #
#######

多少个盒子:5个

###################
#                 #
# ############### #
# #             # #
# # ########### # #
# # #         # # #
# # # ####### # # #
# # # #     # # # #
# # # # ### # # # #
# # # # # # # # # #
# # # # ### # # # #
# # # #     # # # #
# # # ####### # # #
# # #         # # #
# # ########### # #
# #             # #
# ############### #
#                 #
###################

如果有人可以帮助我,我将不胜感激!到目前为止,这是我的代码:

#include <stdio.h>

int main(void){
    int row, column;
    int n;

    printf("How many boxes: ");
    scanf("%d", &n);

    int sideLength;
    sideLength = n * 3 + 1;

    row = 0;
    while(row < sideLength){
        column = 0;
        while(column < sideLength){
            if(row == 0 || row == sideLength - 1){
                printf("#"); 
            }
            else if(column % 2 != 0){
                printf(" ");
            }
            else if(column % 2 == 0 && row % 2 == 1){
                printf(" ");
            }
            else if(column == 0 || column == sideLength - 1 ){
                printf("#");
            }
            else if(column % 2 == 0 && row % 2 == 0){
                printf("#");
            }
            column++;
        }
        printf("%d", column);
        printf("\n");
        row++;  
    }
    return 0;
}

2 个答案:

答案 0 :(得分:0)

#include <stdio.h>

int main (void) {

    int boxes = 0;
    printf("How many boxes: ");
    scanf("%d", &boxes);
    int size = ((boxes * 3) + (boxes - 1));
    int i = 1; // Row counter
    int j = 1; // Column counter
    int mid_row = boxes * 2;

    // For printing each row
    while (i <= size) {
        j = 1;
        if (i < mid_row) {
            // For printing each column
            while (j <= size) {

                // If the current row is odd
                if ((i % 2) != 0) {

                    // If the current column is odd
                    if ((j % 2) != 0) {
                        printf("#");
                    } else {
                        // If the current column is even
                        if ((j < i) || ((size - j) < i)) {
                            printf(" ");
                        } else {
                            printf("#");
                        }
                    }
                } else {
                    // If the current row is even

                    // If the current column is odd
                    if ((j % 2) != 0) {
                        if ((j < i) || ((size - j) < i)) {
                            printf("#");
                        } else {
                            printf(" ");
                        }
                    } else {
                        // If the current column is even
                        printf(" ");
                    }
                }
                j++;
            }
        } else if (i == mid_row) {
            while (j <= size) {
                if ((j % 2) != 0) {
                    printf("#");
                } else {
                    printf(" ");
                };
                j++;
            }
        } else {
            // For printing each column
            while (j <= size) {

                // If the current row is odd
                if ((i % 2) != 0) {

                    // If the current column is odd
                    if ((j % 2) != 0) {
                        printf("#");
                    } else {
                        // If the current column is even
                        if ((j > i) || ((size - j) >= i)) {
                            printf(" ");
                        } else {
                            printf("#");
                        }
                    }
                } else {
                    // If the current row is even

                    // If the current column is odd
                    if ((j % 2) != 0) {
                        if ((j > i) || ((size - j) >= i)) {
                            printf("#");
                        } else {
                            printf(" ");
                        }
                    } else {
                        // If the current column is even
                        printf(" ");
                    }
                }
                j++;
            }
        }

        printf("\n");
        i++;
    }

    return 0;
}

答案 1 :(得分:0)

有很多方法可以解决此问题,但首先必须正确评估最大正方形的印刷面长度。

sideLength = n * 3 + 1;

是错误的,它会生成序列{4,7,10,...},而它应该是{3,7,11,...},因为最小的正方形是3个字符宽,并且它们会以每步4个字符(两边分别是' ''#'

然后,您可以利用所需形状的对称性,将图形分成一个函数内的较小部分

void draw_boxes(int n) {
    const char *stroke = "# ";

    if ( n < 1 )
        return;

    int side = 4 * n - 1;

    for (int row = 0; row < side; ++row) {
        int col = 0;
        // Precalculate the boundaries
        int left = row;
        int right = side - row;
        if ( left > right ) {
            left = right;
            right = row;
        }
        // Left side, alternate '#' with ' '
        for (; col < left; ++col) {
            putchar(stroke[col % 2]);
        }
        // Horizontal line, all '#' or ' ' 
        char ch = stroke[row % 2];
        for (; col < right; ++col) {
            putchar(ch);
        }
        // Right side, same as left side
        for (; col < side; ++col) {
            putchar(stroke[col % 2]);
        }
        putchar('\n');
    }
}

或者,可以计算要在每个位置打印的正确符号,从而可以将前一个功能的图形部分写为

for (int row = 0; row < side; ++row) {
    for (int col = 0; col < side; ++col) {
        bool is_horizontal = 
            ( row < col  &&  col < side - row) ||
            ( side - row <= col  &&  col <= row );
        bool is_space =
            (row % 2  &&  is_horizontal)  ||
            (col % 2  &&  !is_horizontal);
        putchar(stroke[is_space]);
    }
    putchar('\n');
}

逻辑可以进一步缩小,实现某种真值表,但会降低可读性,

void draw_boxes(int n) {        
    const char truth_table[] = "# ##### #     # ";

    if ( n < 1 )
        return;

    int side = 4 * n - 1;

    for (int row = 0, r_row = side - 1; row < side; ++row, --r_row) {
        for (int col = 0; col < side; ++col) {
            unsigned index = (row % 2) << 3  // Is 'row' odd?
                ^ (row < col) << 2           // Is in the top right half?
                ^ (col > r_row) << 1         // Is in the bottom right half?
                ^ (col % 2);                 // Is 'col' odd?

            putchar(truth_table[index]);
        }
        putchar('\n');
    }
}