gpt4 book ai didi

c - 在 C 编程中使用 for 循环打印特定模式

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

嗨,我需要一些帮助来弄清楚如何编写这个模式程序。控制台要求输入多行,并打印出一个由星星组成的上升三角形,其中每行比前一行多两颗星星,然后打印出一个下降三角形,其中每行比前一行少两颗星星。这可能不是最好的解释,但这是我本质上需要得到的一个例子:

输入行数:6

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

另一个奇数的例子:

输入行数:7

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

我一直在论坛上寻找这个具体示例,但尚未找到

最佳答案

这是我的代码:

#include <stdio.h>

int main(int argc, char** argv){
int rows;
int limit;
int twiceFlag = 0;
char c = '*';
int i,j;
printf("enter the number of rows:");
scanf("%d", &rows);

if (rows % 2 == 0){
twiceFlag = 1;
limit = rows / 2;
}
else{
limit = rows / 2 + 1;
}

/*Logic to print the Stars*/
for (i = 1; i <= limit; i++){
printf("%c", c);
for (j = 1; j < i; j++){
printf("%c%c", c, c);
}
printf("\n");
}
if (twiceFlag == 1){
printf("%c", c);
for (j = 1; j < limit; j++){
printf("%c%c", c, c);
}
printf("\n");
}
limit -=2;
for (i = limit; i >= 0; i--){
printf("%c", c);
for (j = i; j > 0; j--){
printf("%c%c", c, c);
}
printf("\n");
}

return 0;
}

关于c - 在 C 编程中使用 for 循环打印特定模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29405012/

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