C 语言编程 - CS50 Mario 金字塔积木打印 - For 循环困难

Programming in C - CS50 Mario pyramid blocks print - For loops difficulties

我正在从 CS50 class 解决这个问题。我还是个初学者。我需要编程的是:

Toward the end of World 1-1 in Nintendo’s Super Mario Brothers, Mario must ascend right-aligned pyramid of blocks, a la the below.

screenshot of Mario jumping up a right-aligned pyramid

Let’s recreate that pyramid in C, albeit in text, using hashes (#) for bricks, a la the below. Each hash is a bit taller than it is wide, so the pyramid itself is also be taller than it is wide.

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

The program we’ll write will be called mario. And let’s allow the user to decide just how tall the pyramid should be by first prompting them for a positive integer between, say, 1 and 8, inclusive.

不过我试过很多方法,其中两个是:

  1. code mariov1

  2. 在查看了一些 Stack Overflow 尝试之后,它现在看起来像这样:

#include <cs50.h>
#include <stdio.h>

string hash(int);

int main(void)
{
    int n;
    do
    {
        n = get_int("Height: ");
    }
    while (n < 0 || n > 8);
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n-1-i; j++)
        {
            for(int j = 0; j < i+1; j++)
            {
                printf(".");
            }
            printf("#");
        }
        printf("\n");
    }
    

}

接下来我可以尝试什么?

Suriyu,补充 Weather Vane 所说的内容。要通过 Check50,您仍然需要对代码进行小的调整,以使其通过所有 CS50 测试。

对于 do-while 循环,当 n = 0 时,n <=0 而不是 n < 0 请求输入,因为规范要求至少一块砖(1 到 8 都包括)。

你只需要这两个循环,不要打印问题集中没有指定的额外字符,例如:printf(".");

祝 CS50 一切顺利,这将是一次有趣的体验!

#include <cs50.h>
#include <stdio.h>


int main(void)
{
    int n;
do
{
    n = get_int("Height: ");
}
while (n < 1 || n > 8);
// this for loop makes new lines
for (int i = 0; i < n; i++)
{
    // here I have two for loops nested inside the above for loop,
    // I previously made the mistake of having two inner loops nested.
    
    // this 2nd for loop prints n-1-i spaces
    // because if n=5, then in the 4th row, there will be 5-1-3 spaces/dots
    for (int j = 0; j < n - 1 - i; j++)
    {
        printf(" ");
    }
    // this 3rd for loop prints i+1 hashes
    // because if n=5, then in the 4th row, there will be 3+1 hashes. 
    // (3 because you count from 0)
    for (int j = 0; j < i + 1; j++)
    {
        printf("#");
    }
    printf("\n");
  }


 }
#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int n;
    do
    {
        n = get_int("Height of the pyramid is:\n");


    }
    while (n < 1 || n > 8); //condition to get a number from 1-8 from the user
    for (int i = 0; i < n; i++) //loop for height
    {
        for (int j = 0; j < n - 1 - i; j++) //loop for spaces on left pyramid
        {
            printf(" ");
        }
        for (int k = 0; k < i + 1; k++) // loop for hashes on left pyramid
        {
            printf("#");
        }
        printf(" "); // spacing between pyramids
        for (int p = 0; p <= i; p++) //loop for right pyramid
        {
            printf("#");
        }
        printf("\n");
    }

}

如果您决定尝试,这是问题的高级版本。

这是一种不同的方法。这个版本不是迭代打印空白,然后迭代打印数字符号,而是创建一个缓冲区(大小由预编译器常量定义 - 当前设置为 8,如果你想允许更大的金字塔,请更改它),然后对于金字塔中的每一行它首先用数字符号填充缓冲区,然后用适当数量的空格覆盖行的开头,然后打印它:

#include <cs50.h>
#include <stdio.h>
#include <string.h>

#define MAXSIZE 8

int main(void)
  {
  int size, spaces;
  char buf[MAXSIZE+1];
  
  do
    size = get_int("Height: ");
  while (size < 0 || size > MAXSIZE);

  buf[size] = '[=10=]';

  for(spaces = size-1 ; spaces >= 0 ; --spaces)
    printf("%s\n", (char *)memset(memset(buf, '#', size), ' ', spaces));
  }

编辑

还有另一种方法,它在内存中的数组中构建整个输出块,然后使用对 puts:

的单个调用打印它
#include <cs50.h>
#include <stdio.h>
#include <string.h>

#define MAXSIZE 8
#define TOTSIZE ((MAXSIZE+1) * MAXSIZE)

int main(void)
  {
  int size, spaces;
  char buf[TOTSIZE+1];
  
  do
    size = get_int("Height: ");
  while (size < 0 || size > MAXSIZE);

  memset(buf, '\n', (size+1)*size);
  buf[((size+1)*size)] = '[=11=]';

  for(char *p = buf, spaces = size-1 ; *p != '[=11=]' ; p += size+1, --spaces)
    memset(memset(p, '#', size), ' ', spaces);

  puts(buf);
  }

这个选项可能效果最好:

从 cs50 导入 get_int

当为真时:

n=get_int("Enter Height: ")

if n>=1 and n<=8:

    break

我在范围(0, n-1):

print(" " * (n - (i+1)) + "#" * (i+1))