catalogue
4. Flip right triangle pattern
5. Right triangle pattern with space
1, Write in front
When brushing the exercises on niuke.com, I suddenly found a series of exercises for printing graphics, and found the charm of printing and the essence of algorithm. Everything in the world can be printed smoothly as long as you can write reasonable code. If you think this blog is good, please collect, like and comment. No more nonsense, let's learn!!!
2, Exercises
1. Line segment pattern
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print line segment patterns composed of "*".
Input:
10
2
Output:
********** **
This problem is still relatively simple. It can be regarded as an entry level. It's OK to cycle
#include<stdio.h> int main() { int i = 0; int j = 0; while(scanf("%d",&j) != EOF) { for(i = 0; i < j; i++) { printf("*"); } printf("\n"); } return 0; }
2, Square pattern
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print square patterns composed of "*".
Input:
4
Output:
* * * * * * * * * * * * * * * *
Input:
5
Output:
* * * * * * * * * * * * * * * * * * * * * * * * *
Like the first question, they are relatively simple, but the following two questions will be derived from here to meet the restrictions of the main cycle.
#include<stdio.h> int main() { int a,j; while(scanf("%d", &a) != EOF) { int i = 0; for(i = 0; i < a; i++) { for(j = 0; j < a; j++) { printf("* "); } printf("\n"); } } return 0; }
3, Right triangle pattern
KiKi learned the cycle, and teacher BoBo gave him a series of exercises to print patterns. The task is to print right triangle patterns composed of "*".
Input:
4
Output:
* * * * * * * * * *
Input:
5
Output:
* * * * * * * * * * * * * * *
When we only print a single graph instead of a spliced graph, we should start printing from the nesting of two loops. After observation, we found that the loop column of the second layer is controlled by the loop. The number of columns increases with the increase of rows. The position occupied by right triangle is equivalent to the general position occupied by 4 * 4 square. Print *
#include<stdio.h> int main() { int n, i, j; while(scanf("%d", &n) != EOF) { for(i = 1; i <= n ; i++) { for(j = 0; j < i ; j++) { if( j == i - 1) printf("*\n"); else { printf("* "); } } } } return 0; }
4. Flip right triangle pattern
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print the inverted right triangle pattern composed of "*".
Input:
5
Output:
* * * * * * * * * * * * * * *
Input:
6
Output:
* * * * * * * * * * * * * * * * * * * * *
The inverted triangle is relatively simple compared with the previous problem. It is observed that Printing can be completed by controlling the * of the n - i column
#include<stdio.h> int main() { int n, i, j; while(scanf("%d", &n) != EOF) { for(i = 0; i < n ; i++) { for(j = 0; j < n - i ; j++) { printf("* "); } printf("\n"); } } return 0; }
5. Right triangle pattern with space
KiKi learned the cycle, and teacher BoBo gave him a series of exercises to print patterns. The task is to print right triangle patterns with spaces composed of "*".
Input:
5
Output:
* * * * * * * * * * * * * * *
Input:
4
Output:
* * * * * * * * * *
As always, compare with the complete square, just find * the law in the table below. This time, print the space first, and then * step by step.
#include<stdio.h> int main() { int n,i,j; while((scanf("%d",&n))!=EOF) { for(i=0;i<n;i++) { for(j=0;j<2*(n-i-1);j++) printf(" "); for(j=0;j<=i;j++) printf("* "); printf("\n"); } } return 0; }
6. Pyramid pattern
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print pyramid patterns composed of "*".
Input:
4
Output:
* * * * * * * * * *
Input:
5
Output:
* * * * * * * * * * * * * * *
This question needs three cycles, the first time to control the number of lines, the second time to print spaces, and the third time to control *. Note that the third control is * for easy alignment.
#include<stdio.h> int main() { int n; while (~scanf("%d\n", &n)) { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n - i - 1; j++) { printf(" "); } for (j = 0; j <= i; j++) { printf("* "); } printf("\n"); } } return 0; }
7. Flip the pyramid pattern
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print the inverted pyramid pattern composed of "*".
Input:
5
Output:
* * * * * * * * * * * * * * *
Input:
6
Output:
* * * * * * * * * * * * * * * * * * * * *
There are many printing methods for this problem. I found the control of J < I by referring to the solution of Table 1. Printing graphics is the problem of finding laws in mathematics. Two cycles are printed directly according to the square, and j < I controls the graphics.
#include<stdio.h> int main() { int n; while (scanf("%d\n", &n) != EOF) { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n ; j++) { if (j < i) { printf(" "); } else { printf("* "); } } printf("\n"); } } return 0; }
8. Diamond pattern
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print diamond patterns composed of "*".
Input:
2
Output:
* * * * * * * * *
Input:
3
Output:
* * * * * * * * * * * * * * * *
After the above two exercises, we can print the upper and lower parts separately for this diamond printing. For details, please refer to the above ideas and pay attention to the number of columns under cyclic control.
#include<stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { int i, j; for (i = 0; i < n + 1; i++) { for (j = 0; j <= n - i - 1; j++) { printf(" "); } for (j = 0; j <= i; j++) { printf("* "); } printf("\n"); } for (i = 0; i < n; i++) { for (j = 0; j <= i; j++) { printf(" "); } for (j = i; j < n; j++) { printf("* "); } printf("\n"); } } return 0; }
9, K-shaped pattern
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print K-shaped patterns composed of "*".
Input:
2
Output:
* * * * * * * * * * *
Input:
3
Output:
* * * * * * * * * * * * * * * * * * *
For this combined graph, it is to print step by step in another large cycle and find the law, which is equivalent to printing two triangles in one cycle, mainly finding the law.
#include<stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { int i, j; for (i = 0; i < n + 1; i++) { for (j = 0; j <n + 1 -i; j++) { printf("* "); } printf("\n"); } for (i = 0; i < n; i++) { for (j = 0; j < i + 2; j++) { printf("* "); } printf("\n"); } } return 0; }
10. Arrow pattern
KiKi learned the cycle. Teacher BoBo gave him a series of practice of printing patterns. The task is to print arrow patterns composed of "*".
Input:
2
Output:
* ** *** ** *
Input:
3
Output:
* ** *** **** *** ** *
This is a little more complicated than the previous calculation, because it is cumbersome to find rules. There are two large cycles, including two cycles of printing. It is also difficult for me to do it for the first time. Finding rules and printing are always wrong, and I have been practicing. Find the law and it will be solved easily.
#include<stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { int i, j; for (i = 0; i < n + 1; i++) { for (j = 0; j < n - i; j++) { printf(" "); } for (j = 0; j < i + 1; j++) { printf("*"); } printf("\n"); } for (i = 0; i < n; i++) { for (j = 0; j < i + 1; j++) { printf(" "); } for (j = n - i; j > 0; j--) { printf("*"); } printf("\n"); } } return 0; }
11. Backslash pattern
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print the backslash pattern composed of "*".
Input:
4
Output:
* * * *
Input:
5
Output:
* * * * *
We found that * is the diagonal of a square, which can be solved by two loops and a judgment statement.
#include<stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { int i, j; for (i = 0; i < n ; i++) { for (j = 0; j < n ; j++) { if (i == j) { printf("*"); } else { printf(" "); } } printf("\n"); } } return 0; }
12. Forward slash pattern
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print the forward slash pattern composed of "*".
Input:
4
Output:
* * * *
Input:
5
Output:
* * * * *
I refer to the solutions of some bloggers and find that the second cycle (J = n; J > I + 1; J --) is better.
#include<stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { int i, j; for (i = 0; i < n; i++) { for (j = n; j > i + 1; j--) printf(" "); printf("*\n"); } } return 0; }
13. X-shaped pattern
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print X-shaped patterns composed of "*".
Input:
5
Output:
* * * * * * * * *
Input:
6
Output:
* * * * ** ** * * * *
This question is the combination of the above two questions. Judge the sentence and read the two conditions.
#include<stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == j || j == n - i - 1) { printf("*"); } else { printf(" "); } } printf("\n"); } } return 0; }
14. Hollow square pattern
KiKi learned the cycle. Mr. BoBo gave him a series of practice of printing patterns. The task is to print "hollow" square patterns composed of "*".
Input:
4
Output:
* * * * * * * * * * * *
Input:
5
Output:
* * * * * * * * * * * * * * * *
This problem is to find the subscript of the square and cycle control.
#include<stdio.h> int main() { int n = 0; while(scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) { if(i == 0 || i == n - 1) { for(int j =0; j < n ;j++) { printf("* "); } } else { printf("*"); for(int j = 0; j < 2 * n - 3; j++) { printf(" "); } printf("* "); } printf("\n"); } } return 0; }
15. Hollow triangle pattern
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print "hollow" triangular patterns composed of "*".
Input:
4
Output:
* * * * * * * * *
Input:
5
Output:
* * * * * * * * * * * *
Find the law of the three sides of the triangle
#include<stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (j == 0 || i == n - 1 || i == j) { printf("* "); } else { printf(" "); } } printf("\n"); } } return 0; }