C模式代码: Why this code for printing Star-Pyramid is not working?

标签 c loops for-loop

我无法弄清楚为什么下面用于打印星金字塔的代码没有给出所需的输出?谁能告诉我哪里出错了?

代码:

#include <stdio.h>

void main(void)
{
    int n, i, j;

    printf("Input height: ");
    scanf("%d", &n);

    for (i = 1; i <= n; i++);
    {
        for (j = 1; j <= 2 * n - 1; j++)
        {
            if ((j >= n - i + 1) && (j <= n + i - 1))
            {
                printf("*");

            }
            else
            {
                printf("");

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

输出:

Input height: 

指定高度后,例如 5。

Input height: 5

控制台输出:

Input height: 5
*********

预期输出:

    *
   ***
  *****
 *******
*********

最佳答案

您的带有注释的代码:

#include <stdio.h>

int main(void)  // main returns int. always *)
{
    int n, i, j;

    printf("Input height: ");
    scanf("%d", &n);  // lets assume we input 5

    for (i = 1; i <= n; i++);  // i gets incremented until it is greater than n --> i = 6, n = 5

    // following lines not indented cause not part of for-loop above.
    { // <-- just a new block beginning, nothing special
    for (j = 1; j <= 2 * n - 1; j++)  // will increment j till it reaches 2*n-1 = 9 with n = 5
    {
        // n - i = -1, -1 + 1 =  0, j starts at 1 --> (j >= n - i + 1) always true.
        // n + i = 11, 11 + 1 = 12, j runs till 9 --> (j <= n + i - 1) always true.
        if ((j >= n - i + 1) && (j <= n + i - 1))
        {
            printf("*");  // will run 9 times
        }
        else
        {
            printf("");  // never.

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

*) 除了独立环境外,您在近距离功能中应该不必担心这一点。

简短版本:

#include <stdio.h>

int main(void)
{
    int height = 0;
    printf("Input height: ");
    scanf("%d", &height);

    for (int h = height; h > 0; --h) {
        for (int i = 1; i <= 2 * height - h; ++i) {
            putchar("* "[i < h]);
        }
        putchar('\n');
    }
}

说明:

height = 8
h = height ... 1                                          2 x
                            h - 1                        height - h
h = 8: 1234567*           7 spaces,  1 star,   8 total = 2 * 8  - 8
h = 7: 123456***          6 spaces,  3 stars,  9 total = 2 * 8  - 7
h = 6: 12345*****         5 spaces,  5 stars, 10 total = 2 * 8  - 6
h = 5: 1234*******        4 spaces,  7 stars, 11 total = 2 * 8  - 5
h = 4: 123*********       3 spaces,  9 stars, 12 total = 2 * 8  - 4
h = 3: 12***********      2 spaces, 11 stars, 13 total = 2 * 8  - 3
h = 2: 1*************     1 space,  13 stars, 14 total = 2 * 8  - 2
h = 1: ***************    0 spaces, 15 stars, 15 total = 2 * 8  - 1

如您所见,空格数就是 h - 1
每行的字符总数为 2 * height - h

因此,在 for 内部循环中,我们计算从 12 * height - h ...的字符总数。对于 i < h,我们打印空格,对于更高的 i 值,我们打印星号。

"* "[i < h] 也可以写成 i < h ? ' ' : '*'
或更详细:

if(i < h) {
    putchar(' ');
} else {
    putchar('*');
}

关于C模式代码: Why this code for printing Star-Pyramid is not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52783939/

相关文章:

python - 如何将数据帧的行与二维数组列表进行比较 - Python

C : how to link a struct to a SEXP pointer? 中的 R 扩展

c++ - Visual Studio 仅在当前文件中转到书签

java - 循环遍历 csv 文件时出现奇怪的行

java - 来自回调/监听器的增量循环

vba - 如何使用 Excel VBA 循环遍历一行中的 5 个单元格

c - 为什么这种隐式转换(在不同指针类型之间)有效?

c - 使用 libav (ffmpeg) 将 RGB 转换为 YUV 使图像一式三份

java - 如何将分钟添加到循环内的日期

Python练习: last letter/first letter