Problem Description
Letters can be used to form some beautiful graphics. An example is given below:
ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC
This is a graph with 5 rows and 7 columns. Please find out the pattern of this graph and output a graph with n rows and m columns.
Input format
Enter a row, which contains two integers n and m, respectively representing the number of rows and columns of the graphics you want to output.
Output format
Output n lines, each m characters, for your graphics.
solution
It is not difficult to see that the letters with coordinates (x, x) are all A, and then extend left and right. But there are many ideas for this question, which can be extended around based on A, or based on the first line, or based on the first column. There are still many ideas, as long as you find a certain rule of the graph output by this question, you can write code according to this rule.
The idea of the code below is to compare the next line with the previous line, remove the tail and add a head. So when writing the code, use the first line to take out the first line [:-1] in the next line, and add a letter before it. In the order of ABCD, add the number to the first line.
Question code
| # Input n rows and m columns n, m = map(int, input().split()) st ='ABCDEFGHIJKLMNOPQRSTUVWXYZ'#Intercept the required section st1 = st[:m]for i in range(1, n + 1) : print(st1) st1 = st[i] + st1[0:-1] |
|---|
Recommended Posts