C模式代码:为什么此用于打印星型金字塔的代码不起作用?

时间:2018-10-12 16:54:01

标签: c loops for-loop

我无法弄清楚为什么以下用于打印Star-Pyramid的代码未提供所需的输出?谁能告诉我我哪里出问题了?

代码:

#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
*********

预期输出:

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

2 个答案:

答案 0 :(得分:0)

清洁图案打印解决方案

#include <stdio.h>
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = n - i - 1; j > 0; j--) {
      printf(" ");
    }
    for (int k = 0; k <= i; k++) {
      printf("*");
    }
    for (int l = 0; l < i; l++) {
      printf("*");
    }
    printf("\n");
  }
  return 0;
}

输入:

6

输出:

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

答案 1 :(得分:0)

您的带有注释的代码:

#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('*');
}