In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "python how to print 9 house grid game", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "python how to print 9-room grid game"!
I believe we have all played the game of nine squares. The rule is to fill in the numbers 1-9 in the nine squares so that the sum of vertical and horizontal oblique addition is equal. It may be a little simple to fill in only nine, but fill in 25, fill in 49, 81 and other odd numbers, and then ask us to find the pattern. How do you code it if you find a pattern?
Before you look at the code, look at this formula:
First in the center of the upward direction, in turn inclined to the right and upward, when the box is out of the box, write down, when the box is out of the right, put it on the left, repeat it back to the next box, and repeat it on the right.
Interpretation: 1 should be placed in the center of the first line, each time according to the upward format to fill in the value, if beyond the upper border to move it vertically down to the last line, if beyond the right box to move it horizontally to the left, in the process of moving if there is already a value to be moved, put this under the previous number, if the upper border and right border are more than, just like the weight, also put this under the previous number. The process is shown below
The code is as follows:
#!/ usr/bin/env python3#-*- coding:utf-8 -*-num = int (input ('please input an odd num')#define a num-length list high = [[0] * num ]#define a num*num list, then assign values to it for i in range(num-1): high += [[0]*num]n = 1#determine the middle value of the first row is 1high[0][num//2] = nx = 0y = num//2#assign values to the high list sequentially starting from 2 for j in range (1,num*num): # x indicates the row number, y indicates the column number, j indicates the x, y coordinate value j = j + 1 x = x - 1 y = y + 1 #Determine which possibilities are met if y > (num - 1) and x
< 0 : x = x + 2 y = y - 1 high[x][y]=j elif x < 0 : x = num - 1 high[x][y]=j elif y >num-1 : y = 0 high[x][y]=j else: if high[x][y] == 0 : high[x][y]=j elif high[x][y] != 0 : x = x + 2 y = y - 1 high[x][y]= j #Print the values in the high list for a in range(num): for b in range(num): # rjust means to output 01, 02, 03, etc. This format can be seen in my previous blog post. print( str(high[a][b]).rjust(2,'0'),end=' ') print() At this point, I believe that everyone has a deeper understanding of "how python prints 9 house grid games", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.