How to draw a spiral of # with only for/while loops and if/else statements (without arrays) using C

匆匆过客 提交于 2019-12-02 13:00:47

I enjoyed this problem and ran into some troubles coming up with an approach. Enough messing around makes the pattern obvious, though; here's the result:

#include <stdio.h>

#define BLANK  "  "
#define FILLED "# "

void spiral(int size) {
    int i, j;

    if (!(size & 1)) { size++; }

    int half = size / 2 + 1;

    /* top */

    for (i = 0; i < half; i++) {
        if (i & 1) {
            for (j = 0; j < size; j++) {
                if (!(j & 1) && (j < i - 1 || j >= size - i)) {
                    printf(FILLED);
                }
                else {
                    printf(BLANK);
                }
            }
        }
        else {
            for (j = 0; j < size; j++) {
                if ((j & 1) && (j < i - 1 || j >= size - i)) {
                    printf(BLANK);
                }
                else {
                    printf(FILLED);
                }
            }
        }

        puts("");
    }

    /* bottom half */

    for (; i < size; i++) {
        if (i & 1) {
            for (j = 0; j < size; j++) {
                if (!(j & 1) && (j < size - i || j > i)) {
                    printf(FILLED);
                }
                else {
                    printf(BLANK);
                }
            }
        }
        else {
            for (j = 0; j < size; j++) {
                if ((j & 1) && (j < size - i || j > i)) {
                    printf(BLANK);
                }
                else {
                    printf(FILLED);
                }
            }
        }

        puts("");
    }
}

int main() {
    int i;

    for (i = 1; i < 35; i += 2 ) {
        spiral(i);
        puts("\n");
    }

    return 0;
}

Output:

# 


# # # 
    # 
# # # 


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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

Explanation:

Let i represent the current row index and j represent the current column index.

Split the problem into two halves. The bottom half is easier because the left and right halves are symmetrical, so both even and odd rows print their respective pattern on alternating column indices if j < size - i || j > i.

The top half is a little trickier: the pattern is the same, except the left side lags behind the right side by 1, meaning we need to subtract 1 from the left side: if j < i - 1 || j >= size - i.

Note that this code could use some cleanup to reduce repetition; I left it verbose so that it's clear how it works. I also took the liberty of adjusting the characters to print because the original is harder on the eyes--this is easily adjusted using the #defines or adding parameters to the function.

Here's a repl.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!