* "in the know turned professional success after I began to all aspects of the preparation To the May Day holiday is very timely, just give me the time of learning C language foundation (although I don't like paid leave, but the number of course, no paid leave should be about the same, habit good) "*" printing hollow pyramid "this is my in learning C language program, # print a hollow pyramid ## By analyzing the output, gradually deduce the method ### Process 1: Print a solid pyramid with n layers using the for loop print a solid pyramid with four layers * -- first layer, 1 "*", first half n-1 "* ** -- second layer, 3 "*", first half n-2 "***** -- third layer, 5 "*", the first half n - 3 "" * * * * * * * -- the fourth floor, 7" * ", the first n - 4 "" and so on are:... -- layer N-1, [2(n-1)-1] "*", first half of n- (n-1) ""...... -- layer n, (2n-1) "*", first n-n "" :Copy the code
void main() { int i, j, k, n; Printf (" Please enter the number of layers of the pyramid you wish to print \n"); scanf_s("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n - i; j++) { printf(" "); } for (k = 1; k <= (2 * i - 1); k++) { printf("*"); } printf("\n"); } system("pause"); }Copy the code
The running results are as follows:Copy the code

### Process 2: First print a four-layer hollow pyramid * -- the first layer, the first half of n-1 "", between the first "*" and the last "*" there are 0 "* * -- second layer, the first half of n-2" ", First "*" and the last one between "*" "* *" - the third layer, the first n - 3 ", "the first" * "and the last one to three" "between" * "* * * * * * * -- the fourth floor, the first n - 4", "7" * "and so on are: printing... - n - 1 layer, the first half of n - (n - 1) ", "the first" * "and the last one between a" * "[2] (n - 1) - 1 a" "...... -- layer n, first half of n-n "", print (2n-1) "*" code as follows:Copy the code
void main() { int i, j, k, n; Printf (" Please enter the number of layers of the pyramid you wish to print \n"); scanf_s("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n - i; j++) { printf(" "); } if (i < n) for (k = 1; k <= (2 * i - 1); k++) { if (k == 1 || k == (2 * i - 1)) printf("*"); else printf(" "); } else for (k = 1; k <= (2 * i - 1); k++) { printf("*"); } printf("\n"); } system("pause"); }Copy the code
The running results are as follows:Copy the code

# improvement The code above is my individual thinking out the implementation of the method, by running the code above, indeed can output a hollow pyramid But some places written is trival, concision to save time In my study, through to the teacher in the course of understanding, that there was some places can be improved Such as print, "*", There is no need to determine how many "" there are between the two "*", just print "*" where it is needed, and print" "elsewhere. The specific code replacement is as followsCopy the code
    if (i < n)
        for (k = 1; k <= (2 * i - 1); k++)
        {
            if (k == 1 || k == (2 * i - 1))
                printf("*");
            else
                printf(" ");
        }
    else
        for (k = 1; k <= (2 * i - 1); k++)
            printf("*");
Copy the code
Replace withCopy the code
    for (k = 1; k <= (2 * i - 1); k++)
    {
        if(k==1||k== (2 * i - 1)||i==n)
        printf("*");
        else
        printf(" ");
    }
Copy the code
This not only makes the code more concise, but also saves the number of judgment statements, saving time and space. The code after replacement is as follows, and the running result is the same as aboveCopy the code
void main() { int i, j, k, n; Printf (" Please enter the number of layers of the pyramid you wish to print \n"); scanf_s("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n - i; j++) { printf(" "); } for (k = 1; k <= (2 * i - 1); k++) { if(k==1||k== (2 * i - 1)||i==n) printf("*"); else printf(" "); } printf("\n"); } system("pause"); }Copy the code
# Summary: This project mainly investigates the use of loop statements, for, while statements can be implemented, the principle is the same. It was easy, but it was the first project I ever completed. I'm Lemon, and I'm here to chronicle my journey. Thank you for reading, and I'll see you next time.Copy the code