gpt4 book ai didi

c - 如何在三角形中排列星号

转载 作者:太空宇宙 更新时间:2023-11-04 03:21:58 25 4
gpt4 key购买 nike

我需要能够打印一个带星号的直角三角形,让用户像这样输入 n 行并仅使用 for 循环(这是大学的练习).

enter number of rows: 5.
*****
****
***
**
*

到目前为止,这是我能够做的并且它输出了这个

5 rows:
*
**
***
****
*****

代码:

int main(void)
{
int n, i, j;

printf("Ingrese la cantidad de filas:\n");
scanf("%d", &n);

for (i = 0; i < n; i++)
{
for (j = 0; j < i+1; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

最佳答案

应该这样做:

#include <stdio.h>
int main(void)
{
int n, i, j;
printf("Ingrese la cantidad de filas:\n");
scanf("%d", &n);
for (i = n; i > 0; i--){
for (j = n - i; j > 0; j--){
printf(" "); //print the blank spaces before the stars
}
for(j = i; j>0; j--){
printf("*"); //print the stars
}
printf("\n"); //next line
}
return 0;
}

例如:输入 = 5

输出:

*****
****
***
**
*

关于c - 如何在三角形中排列星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44764678/

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