c - 无法右对齐马里奥问题集的金字塔

标签 c cs50

我目前正在做 CS50 第一周的马里奥问题(不太舒服)。到目前为止,我可以打印金字塔的哈希值,但我无法用空格右对齐它。有人能够指出我哪里出了问题吗?预先非常感谢您。

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

int main(void)
{
int hashes;
int space;
int height;

do 
{
    height = get_int("height: ");
}
while (height < 0 || height > 5);
{
    for (int i = 0; i <= height; i++)
    {
        for (space = (height - 1); space <= 0; space++)
        {
            printf(" ");
        }

        for (hashes = 1; hashes <= i; hashes++)
        {
            printf("#");
        }
        printf("\n");

    }


  }

}

最佳答案

正如Antti所说,在你的“空间”循环中,在i的第一次迭代中(因此i = 0),height - 1永远不会<或= 0(除非高度是设置为 1)。

如果高度设置为 5,则第一行 # 之前应打印多少个空格?是4吗?

那么,如果第一行 int Space = 0,height = 5,什么样的循环代码会得到 4 的结果?但是考虑一下 Space 的下一次迭代,如果 int Space = 1,Height = 5,什么循环代码可以获得结果 3?

 for (space = 0; "this needs to to = 4" ; space++)
        {
            printf(" ");
        }
 for (space = 1; "this needs to to = 3" ; space++)
        {
            printf(" ");
        }

“空格”的值在每次迭代时都会发生变化,您可以使用它吗?

关于c - 无法右对齐马里奥问题集的金字塔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57283901/

相关文章:

c - C中的贪婪算法(带硬币)

c - 端口正在监听但 "No connection could be made because the target machine actively refused it "

C: 如何用更新的 GtkWidget 对齐属性替换 GtkAlignment 函数

python - 使用脚本或正则表达式将 "printf"添加到每个函数

c - (C 套接字编程)来自服务器的单独 send() 调用在同一个客户端 recv() 缓冲区中结束

c - 为什么这段代码要在假设第一个元素最小的情况下查找数组中的最小数?

无法获取 '' 的值 i '' on the second ' 'FOR''

c - C. 段错误中的十五局游戏

c - gethostbyname 有什么问题?

c - 为什么会出现 'invalid or unsupported image format'错误?