gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:35:16 25 4
gpt4 key购买 nike

所以我正在做 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com