问题
So I've looked up and only found normal triangles questions out there. This one is a bit tricky.
Given a N, your program should create an asterisk unfilled triangle with a N side size.
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");
}
}
回答1:
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
******
* *
* *
* *
**
*
回答2:
You need to think about an algorithm for printing out the triangle. There are three separate types of lines you (may) need to draw:
- A row full of N asterisks
- A row with an asterisk at the start, one at the end and N-2 spaces in the middle.
- A row with 1 asterisk (which is just a subset of #1).
Step 1 - work out how to print out the above rows
Step 2 - work out when to print out the above rows
回答3:
a longer version of this problems which can be optimized a great deal
void print_start_and_end_asterix( int n )
{
int i = 0;
for ( ; i < n ; i ++ )
{
if( i == 0 || i == n-1 )
printf("*");
else
printf(" ");
}
printf("\n");
}
void print_full_row( int n )
{
int i = 0;
for ( ; i < n ; i ++ )
printf("*");
printf("\n");
}
int main (){
int n;
scanf("%d", &n);
int row = 0 , col = n;
for ( row = 0 ; row < n ; row ++ , col--) {
if( row == 0 )
print_full_row(col);
else
print_start_and_end_asterix(col);
}
return 0;
}
回答4:
I'm just providing you a logic , you can work upon it . You can take three loops. One for row say i , second for * (asterisk ) say j , and third for the white spaces ( empty triangle) say k. Now , I hope you know how to make a triangle by using i,j . The logic for k could be it should be executed 1 less then the rows' loop(i.e. the jth one) and should print a blank space(" ").
You can divide your Problem in the modules like this
print_asterisk(n);
print_hollow_asterisk();
print_asterisk(1);
回答5:
The triangle being drawn is a two-dimensional figure, and therefore can be drawn with two nested for
loops. The outer loop keeps track of the row which is being drawn. The inner loop keeps track of the column. For each combination of row
and col
, a very simple decision must be made, shall we output an asterisk, or shall we output a space.
Begin with the outer loop. It makes sense for the outer loop to count down from N to 1, since the number of characters on a line starts at N and decreases till it reaches 1. So the outer loop is
for ( row = N; row >= 1; row-- )
Next is the inner loop. The inner loop needs to count the number of characters on each line. We've arranged things so that the row
variable in the outer loop is equal to the number of characters. Hence, the inner loop is simply
for ( col = 0; col < row; col++ )
Now we come to the decision. Given a row
and a col
, shall we print an asterisk or a space? Well, we should always print an asterisk on the first line, which is where row == N
. We should also print an asterisk on the left edge, where col == 0
. Finally, we need an asterisk on the right edge, where col == row-1
. Hence, the decision code is
if ( row == N || col == 0 || col == row-1 ) // top or left or right
putchar( '*' );
else
putchar( ' ' );
Finally, after the inner loop is finished, we need to output a newline character to start the next line
putchar( '\n' );
And that's all there is to it.
来源:https://stackoverflow.com/questions/32858331/print-empty-asterisk-triangle-c