c - C 中 ' # ' 字符的右对齐

标签 c cs50

所以我正在做 cs50 问题集,但我陷入了输出中字符的右对齐问题。

我的程序(mario.c)的代码是:

#include<stdio.h>

int main(void)
{
    int height=-1;

    while(height<0 || height>23)
    {
        scanf("%d",&height);
        printf("height: %d\n",height);
    }

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

这是我想要的输出:

Output ( that i want )

以及我得到的输出:

Output ( that i am getting )

请帮帮我。提前致谢。

最佳答案

printf 首先是空格。

for(int i = 1; i <= height; ++i)
{
    for (int k = 1; k <= height - i; ++k)
        printf(" ");
    for(int j = 1; j <= i + 1; ++j)
    {
        printf("#");
    }
    printf("\n");
}

关于c - C 中 ' # ' 字符的右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39956190/

相关文章:

c - Scanf 查找整数

c - 控制可能会达到非无效功能的终点。 C

c - 在 CS50 库中使用字符串

c - 永远运行的贪心程序

子进程不会因kill(pid_t, SIGTERM)而死亡

C: 低级字符格式化:(enter+newline) 使用 fgetc

c - fread 返回与 nmemb 相同的数字,但已到达文件末尾

c - "Vigenere": String lowercasing program is appending character for some inputs

c - 使用 C 使用 libFLAC 解码数据

c - strcpy 错误返回无响应窗口