c - 打印马里奥二号半金字塔 CS50

标签 c cs50

嘿,我正在解决 CS50 更舒适的问题,但我不知道如何在同一行上打印第二个马里奥金字塔。在我的代码中它已经打印出来,但它不在同一行。

你是否指导我或告诉我如何去做并不重要。我正在使用 CS50 作为练习,我不会上交任何东西。所以这不会是作弊。

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    int height = 0;

    // left pyramid variables
    int i = 0;
    int j = 0;
    int k = 0;

    // variable for gap
    int g = 0;

    // right pyramid variables
    int l = 0;
    int m = 0;
    int n = 0;


    //do - while loop -- works

    do
    {
        printf("Height: ");
        scanf("%d", &height);
    }

    while (height < 0 || height > 23);

    // Print pyramids

        // print spaces for left pyramid (less spaces needed with time) ✓
        // print hashes for left pyramid ✓
        // print gap (2)
        // print hashes for right pyramid
        // print new line - for next row

    // Left Pyramid

    // Rows -- determines the height
    for (i = 0; i < height; i++)
    {
        // Cols -- in this one we are doing the spaces

        // the -i makes it left aligned -- to make it right aligned remove the "-1"
        for (j = 0; j < height-i; j++)
        {
            // Printing Spaces
            printf(" ");
        }
        // "i+1" - we want i to be 1 whenever height is 0, and we want i to increase by one
        // whenever the height increases, so that's why we add + 1 to it
        // if I don't add 1 to it what it does is that prints a new line, and then it prints
        // 4 things instead of 5 for example.
        for (k = 0; k < i + 1; k++)
        {
            printf("#");
        }

        // Print new line
        printf("\n");
    }

    // Gap -- fix gap, the rest works how it should -- I think I need to make everything
    // inside one loop

    // for (g = 0; g < height; g++)
    // {
    //     printf("  ");
    // }

    // Right Pyramid

      // Rows -- determines the height
    for (l = 0; l < height; l++)
    {

        // Cols -- in this one we are doing the spaces

        // right aligned
        for (m = 0; m < height; m++)
        {
            // Printing Spaces
            printf(" ");
        }
        // "i+1" - we want i to be 1 whenever height is 0, and we want i to increase by one
        // whenever the height increases, so that's why we add + 1 to it
        // if I don't add 1 to it what it does is that prints a new line, and then it prints
        // 4 things instead of 5 for example.
        for (n = 0; n < l + 1; n++)
        {
            printf("#");
        }

        // Print new line
        printf("\n");
     }


    return 0;
}

最佳答案

为什么不直接在第一个循环中执行类似的操作:

for (k = 0; k < i + 1; k++)
{
    printf("#");
}

/* this is new */
/* Draw the gap */
for (k = 0; k < gap; k++) {
    printf(" ");
}
/* Draw the left part*/
for (k = 0; k < i + 1; k++)
{
    printf("#");
}

关于c - 打印马里奥二号半金字塔 CS50,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43860533/

相关文章:

c - 在 C 中使用结构

c - 双向链表搜索函数的C实现

c - 为什么我的 PSET1 Credit 可以在沙盒中运行,但不能在 Check50 中运行?

c - 解释一下我的 C 代码的结果。 K&R "The C Prog language"Ex1-6

c - 如何找到 sizeof 数组?

CS50 拼写器 : Unload executes 80, 000,000+ 免费

c - 将数值变量视为文本

CS50 pset2 Vigenere 代码 - 输出一个不正确的字母

java - Android jni GetMethID 崩溃

c - 在 C 中创建自定义 .txt 文件名时出现问题