Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打印出马里奥二号半金字塔CS50_C_Cs50 - Fatal编程技术网

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

打印出马里奥二号半金字塔CS50,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

嘿,我正在研究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;
}
#包括
#包括
内部主(空)
{
整数高度=0;
//左金字塔变量
int i=0;
int j=0;
int k=0;
//间隙变量
int g=0;
//右金字塔变量
int l=0;
int m=0;
int n=0;
//do-while循环——有效
做
{
printf(“高度:”);
扫描频率(“%d”和高度);
}
而(高度<0 | |高度>23);
//印刷金字塔
//打印左棱锥体的空格(随着时间的推移所需的空格越来越少)✓
//左棱锥体的打印哈希✓
//打印间隙(2)
//右棱锥体的打印哈希
//打印新行-下一行
//左金字塔
//行--确定高度
对于(i=0;i
为什么不在第一个循环中执行以下操作:

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("#");
}
(k=0;k { printf(“#”); } /*这是新的*/ /*缩小差距*/ 对于(k=0;k恰当地命名变量:而不是将“//variable”表示为gap”的注释。。调用变量“gap”!或者更具描述性的东西我被代码注释淹没了!!由于换行结束一行,没有返回到前一行的选项(好吧,不使用基础知识),您只需同时打印出左右两行;你查过了吗?@Evert我是怎么查的?是的,我查过了。人们决定做普通的版本。我还没有找到一个更舒适的版本,就是这个,我会试试那个。我还必须包括每个#之间的空间,以及左右金字塔之间的两个空间间隙。谢谢你的意见。我很感激,谢谢你。成功了。唯一不起作用的是两个金字塔之间的两个间隙。但是剩下的工作:D你能帮我解决两个空格吗?NVM我计算出了这个空格,我做了如下操作:for(k=0;k<1;k++){printf(“”;}