打印"三角"星号 [英] Printing a "triangle" of asterisks
问题描述
我应该写入应该打印三角形以下面的方式的方案:
如果行数是2:
*
***
如果行数为3:
*
* *
*****
但是,以下code,我做到了打印星最后一行正确的金额,但我不敢肯定我会如何打印空间和换行符。我的code打印底层明星如下:
无效tri_func(NUM)
{ INT行; INT C = 1;
诠释J; 为(行= 1;行< = NUM;排++)
{
为(J = 1; J&下;行2; J ++)printf的();
为(C = 1; C< =(2 *行) - 1-J,C ++)
{
的printf(*);
} 的printf(\\ n);
}
}
好吧,如果你有 N
行,第一行应该包含ñ -1
空格和 *
。在 I
日线(1< I< N)应该包含 NI
空格,一个 *
, 2I-3
空格和另一个 *
,最后行应包含 2N-1
*
秒。您可使用循环很容易地做到这一点。要打印空间,使用的printf();
,记得在结束时打印 \\ n
每一行。
The program I am supposed to write is supposed to print the triangle in the following manner:
If the number of rows is 2:
*
***
If the number of rows is 3:
*
* *
*****
However, the following code that I did prints the correct amount of stars for the last line, but I am not so sure how I would print the space and the newline. My code for printing the bottom level stars is the following:
void tri_func(num)
{
int row; int c=1;
int j;
for ( row = 1 ; row <= num ; row++ )
{
for (j=1; j < row-2; j++) printf(" ");
for ( c = 1 ; c <= (2*row )- 1-j ; c++ )
{
printf("*");
}
printf("\n");
}
}
Well, if you have n
lines, the first line should contain n-1
spaces and a *
. The i
th line (1< i < n) should contain n-i
spaces, a *
, 2i-3
spaces and another *
, and the last line should contain 2n-1
*
s. You can easily do it using loops. To print a space, use printf(" ");
, and remember to print a \n
in the end of every line.
这篇关于打印&QUOT;三角&QUOT;星号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!