作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一名新生,我们有一个简介事件。我们的任务是使用循环创建一棵圣诞树...
我的代码在这里:
#include<stdio.h>
int main ()
{
int rows,a,b,space;
clrscr();
printf("Enter a number of rows:");
scanf("%d",&rows);
space=rows-1
for(b=space;b>=1;b--)
{
for(a=rows;a>=1;a--)
space--;
printf("");
for(a=2*(rows-b)-1;a>=1;a--)
printf("*",a);
printf("\n");
space = space-1;
}
getche();
return 0;
}
这段代码是我们的教授给我们的...程序运行了,但是输出是错误的。你能帮我吗?
当我运行这个程序时,输出如下:
*
***
*****
******
*******
最佳答案
你必须找到一种模式。假设您想要一棵包含 n
行的树。最后一行将有 2n-1 颗星。其前面的行将有 2n-3
等等。要打印一行,首先打印一些空格,然后打印一些星号。对于最后一行,您打印 0
空格和 2n-1
星号。对于其之前的行,您可以打印 1
空格和 2n-3
星号,依此类推。
for(int i = 0; i < n; i++)
{ for(int j = i + 1; j < n; j++)
printf(" ");
for(int j = 0; j <= 2*i; j++)
printf("*");
if(i < n - 1) puts("");
}
关于c - 如何在C程序中使用循环制作圣诞树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26204277/
我是一名优秀的程序员,十分优秀!