【问题标题】:Wrong output when printing a tent shape with stars打印带有星星的帐篷形状时输出错误
【发布时间】:2017-09-11 23:40:55
【问题描述】:

我正在尝试使用星号“*”打印出一个空心的开放式帐篷形状。该代码使用两个 for 循环,第一个用于行,另一个用于列。

以下是我的代码:

void printTent(int n)
{   
    int j = 1;
    int i = 1;

    if (n == 1) {
        printf("*");

    } else {
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++) {
                printf(" ");
            }
            if(j == n) {
                printf("*");
                for(j = 1; j <= n; j++) {
                    printf(" ");

                }
            }
        }
    }
}


int main()
{
    printTent(4);
}

得到的输出:

    *        *        *        *    

期望的输出:

   *
  * *
 *   *
*     *

【问题讨论】:

标签: c for-loop asterisk triangular


【解决方案1】:

我认为你不需要那个

if (n == 1) {
    printf("*");
}

我们可以处理您在else 部分中所写的内容。

对于n=4,每行开头要打印的空格数为 3、2、1 和 0。

您似乎正试图通过您的第一个内部循环来实现这一点。但是

for(j = 0; j < n; j++) {
    printf(" ");
}

将始终打印n 空格。我们需要在外循环的每次迭代中减少1 打印的空格数。

进入第二个循环,

for(j = 1; j <= n; j++) {
    printf(" ");
}

这有一个类似的问题,唯一的区别是打印的空格数的增加。

试试这样的

void printTentNMMod(int n)
{
    int j;
    int i;

    for(i = 0; i < n; i++) {
        for(j = i; j < n; j++) {
            printf(" ");
        }
        printf("*");
        if(i!=0)
        {
            for(j=0; j<2*(i-1)+1; ++j)
            {
                printf(" ");
            }
            printf("*");
        }
        printf("\n");

    }
}

此外,您可以将其缩短为

void printTent(int n)
{
    int j;
    int i;

    for(i = 0; i < n; i++) {
        printf("%*c", n-i, '*');
        if(i!=0)
        {
            printf("%*c", 2*i, '*');
        }
        printf("\n");

    }
}

%*c 中的* 将设置%c 打印的字符所占据的位置数。

【讨论】:

    【解决方案2】:

    我已经完成了,我已经写了注释。

    void printTent(int n)
    {
        int j = 1;
        int i = 1;
    
        if (n == 1) {
            printf("*");
    
        }
        else {
            for (i = 0; i < n; i++) {
                for (j = 0; j < n -i; j++) {// you should use n-i instead of n because the number of spaces is decreasing
                    printf(" ");
                }
                if (j == n-i) { //
                    printf("*");
                    for (j = 1; j <= i * 2 - 1; j++)//this loop outputs spaces between two "*"
                    {
                        printf(" ");
                    }
                    if (i != 0)//the first line only needs one "*"
                        printf("*");
                    printf("\n"); //Line breaks
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      另一种方式。

      #include <stdio.h>
      
      int main() {
          int i, j;
          int height = 5;
          for(i = height; i > 0; i--) {
              for(j = 1; j < height * 2; j++) {
                  if(j == i || j == height * 2 - i)
                      printf("*");
                  else
                      printf(" ");
              }
              puts("");
          }
          return 0;
      }
      

      输出

          *    
         * *   
        *   *  
       *     * 
      *       *
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-26
        • 1970-01-01
        • 1970-01-01
        • 2019-11-06
        • 1970-01-01
        相关资源
        最近更新 更多