Printing drawings (circular nesting special exercise)

The design intention of this paper: the preparation of the special exercise of printing graphics is to help students further understand and master the design and preparation of loop nesting, with the focus on learning to analyze the relationship between loop variables

Tip: when learning programming, the biggest bad habit for beginners is not to use draft paper. Their eyes are high and their hands are low. Before they get the title and think about the algorithm, they move the handwritten program and look at it step by step. In this way, the written program will often have various problems, and eventually waste a lot of time for debugging. When the problem is difficult and there is no idea in the later stage of learning, We can only sit in front of the computer in a daze. There is no draft paper for math practice. We can also write and draw on the test paper. If there is no draft paper for programming, we can only fall into fantasy. Please avoid it!!!
When you encounter difficulties in thinking about problems, pen and paper will be your most powerful weapons, which can help you chop out your algorithm ideas.

1. Rectangle

The plot is as follows
******
******
******
******
******
******

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Analysis: it is not difficult to think of using loop nesting to print the graphic matrix of 6 rows and 6 columns. The outer loop controls the number of rows printed, and the inner loop controls the number of rows printed per row,.
Note: each "*" does not wrap after printing, so end = "" needs to be added. Each line needs to wrap after printing. The function of print() is to wrap
The final standard range is as follows:

for i in range(6):			#Number of rows
	for j in range(6):		#Number of columns
		print("*",end="")	#Each "*" does not wrap after printing: end = ""
	print()					#Wrap after printing a line

   
  • 1
  • 2
  • 3
  • 4

2. Right triangle 1

The plot is as follows
*
**
***
****
*****
******

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Tip: it is suggested that the value of the outer loop variable of this problem should start from 1. If it starts from 0, the inner loop should start from - 1

for i in range(1,7):
	for j in range(i):
		print("*",end="")
	print()

   
  • 1
  • 2
  • 3
  • 4

3. Right triangle 2

The plot is as follows
******
*****
****
***
**
*

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Tip: this question is not as easy to see the results intuitively as the above two questions. At this time, we need to take out the draft paper and list the value changes of internal and external circulation variables on the paper

ij
16
25
34
43
52
61

From the above table, we can analyze the relationship expression of internal and external loop variables: j=7-i

for i in range(1,7):
	for j in range(7-i):
		print("*",end="")
	print()

   
  • 1
  • 2
  • 3
  • 4

4. Right triangle 3

The plot is as follows
     *
    **
   ***
  ****
 *****
******

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Tip: this question still prints 6 lines of "*", but each line needs to print spaces first and then "*", so the code framework is as follows:

for i in range(1,7):
	#Cycle through spaces
	for k in range(_____):
		print(" ",end="")
	#Cyclic printing*
	for j in range(_____):
		print("*",end="")
	print()

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Then list the value changes of internal and external circulation variables

iSpace kAsterisk j
151
242
333
424
515
606

From the above table, we can analyze the relationship expression of internal and external loop variables as follows:
k=6-i
j=i
Therefore, the code is as follows:

for i in range(1,7):
	#Cycle through spaces
	for k in range(6-i):
		print(" ",end="")
	#Cyclic printing*
	for j in range(i):
		print("*",end="")
	print()

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

5. Right triangle 4

The plot is as follows
******
 *****
  ****
   ***
    **
     *

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

List the value changes of internal and external circulation variables

iSpace kAsterisk j
106
215
324
433
542
651

From the above table, we can analyze the relationship expression of internal and external loop variables as follows:
k=i-1
j=7-i
Therefore, the code is as follows:

for i in range(1,7):
	#Cycle through spaces
	for k in range(i-1):
		print(" ",end="")
	#Cyclic printing*
	for j in range(7-i):
		print("*",end="")
	print()

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

6. Isosceles triangle 1

The plot is as follows
     *
    ***
   *****
  *******
 *********
***********

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

List the value changes of internal and external circulation variables

iSpace kAsterisk j
151
243
335
427
519
6011

From the above table, we can analyze the relationship expression of internal and external loop variables as follows:
k=6-i
j=2*i-1
Therefore, the code is as follows:

for i in range(1,7):
	#Cycle through spaces
	for k in range(6-i):
		print(" ",end="")
	#Cyclic printing*
	for j in range(2*i-1):
		print("*",end="")
	print()

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

7. Isosceles triangle 2

The plot is as follows
***********
 *********
  *******
   *****
    ***
     *

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

List the value changes of internal and external circulation variables

iSpace kAsterisk j
1011
219
327
435
543
651

From the above table, we can analyze the relationship expression of internal and external loop variables as follows:
k=i-1
j=13-2*i
Therefore, the code is as follows:

for i in range(1,7):
	#Cycle through spaces
	for k in range(i-1):
		print(" ",end="")
	#Cyclic printing*
	for j in range(13-2*i):
		print("*",end="")
	print()

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

8. Diamond three methods (all three methods)

The plot is as follows
     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Method 1: it can be seen in the figure that k and j are symmetrical about line 6, so the value changes of internal and external circulation variables are listed

iSpace kAsterisk jabs(i-6)
1515
2434
3353
4272
5191
60110
7191
8272
9353
10434
11515

From the above table, we can analyze the relationship expression of internal and external loop variables as follows:
k=abs(i-6)
j=11-2*abs(i-6)
Therefore, the code is as follows:

for i in range(1,12):
	#Cycle through spaces
	for k in range(abs(i-6)):
		print(" ",end="")
	#Cyclic printing*
	for j in range(11-2*abs(i-6)):
		print("*",end="")
	print()

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Method 2: because the diamond is a symmetrical figure, it is considered to take the symmetrical value of loop traversal i from - 5 to 5 (range(-5,6)), so the values of internal and external loop variables are listed as follows

iSpace kAsterisk j
-551
-443
-335
-227
-119
0011
119
227
335
443
551

From this table, we can easily analyze the relationship between internal and external circulation variables as follows:
k=abs(i)
j=11-2*abs(i)
The final code is as follows:

for i in range(-5,6):
	#Cycle through spaces
	for k in range(abs(i)):
		print(" ",end="")
	#Cyclic printing*
	for j in range(11-2*abs(i)):
		print("*",end="")
	print()

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Method 3: Manhattan distance: indicate the sum of the absolute wheelbase of two points in the standard coordinate system. For example, on the plane, the Manhattan distance between point i of coordinate (x1,y1) and point j of coordinate (x2,y2) is:
d(i,j)=|X1-X2|+|Y1-Y2|
It is not difficult to see that the Manhattan distance from all points in the diamond to the center point (6,6) is less than or equal to 5
The scale range is as follows:

for i in range(1,12):
	for j in range(1,12):
		if(abs(i-6)+abs(j-6)<=5):
			print("*",end="")
		else:
			print(" ",end="")
	print()

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Tip: isosceles triangle can also be printed according to "Manhattan distance". Please try it yourself

9. Round

Title Description: print a "*" circle with a radius of 50. (do not use the matplotlib module to plot)

Tip: similar to Manhattan distance
The scale range is as follows:

for i in range(1,100):
	for j in range(1,100):
		if((i-50)**2+(j-50)**2<=50**2):
			print("*",end="")
		else:
			print(" ",end="")
	print()

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Tip: if the screen cannot display a circle, you can turn down the font, but you may see a less smooth "ellipse" (manual funny), because the output code (font code) of "*" is not a square, but a rectangular dot matrix, and there is a certain gap between lines.
Python has a special drawing library, such as matplotlib, for drawing, which will be explained in subsequent teaching.

10. Autonomous practice

The plot is as follows
***********
 *********
  *******
   *****
    ***
     *
    ***
   *****
  *******
 *********
***********

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Methods: referring to the previous description in this paper, first list the value changes of internal and external cyclic variables, then analyze the relationship expression between cyclic variables from the table, and write the code after the algorithm design is completed.

Tags: C++

Posted on Fri, 01 Oct 2021 14:33:46 -0400 by ihsreal