问题描述
所以,我抬头一看,才发现,正常的三角形问题在那里。
这一个是有点棘手。
Example 1
Inform N: 1
*
Example 2:
Inform N: 2
**
*
Example 3:
Inform N: 3
***
**
*
Example 4:
Inform N: 4
****
* *
**
*
Example 5:
Inform N: 5
*****
* *
* *
**
*
Here's my attempt, I could only make a filled triangle inefficiently
void q39(){
int n,i,b;
printf("Inform N: ");
scanf ("%i",&n);
for ( i = 0; i < n; ++i)
{
printf("*");
for ( b = 1; b < n; ++b)
{
if (i==0)
{
printf("*");
}
else if (i==1 && b>1)
{
printf("*");
}
else if (i==2 && b>2)
{
printf("*");
}
else if(i==3 && b>3){
printf("*");
}
else if(i==4 && b>4){
printf("*");
}
else if(i==5 && b>5){
printf("*");
}
else if(i==6 && b>6){
printf("*");
}
else if(i==7 && b>7){
printf("*");
}
else if (i==8 && b>8){
printf("*");
}
}
printf("\n");
}
}
you just need to think that first line should be filled with *
.
Second thing is first character of every line should be *
.
and last character should also be *
. and in between you need to fill spaces.
int main()
{
int n=6;
for(int i=n-1;i>=0;i--) // using n-1, bcz loop is running upto 0
{
for(int j=0;j<=i;j++)
{
if(i==n-1 || j==0 ||i==j)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
The condition if(i==n-1 || j==0 ||i==j)
here i==n-1
is used so that first line should be filled with *
.j==0
is used to make first character of every line *
. every time when new line starts i.e j=0 it will print one *
character.i==j
this is used to make last character *
when i==j that is last index upto which we are running loop. so at last index it will print a *
.
And for all other values it will print space as it will run else
condition.
OUTPUT
******
* *
* *
* *
**
*
这篇关于打印空星三角Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!