Blue Bridge Cup letter graphics (simulation)

1. 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 law of this graph and output a graph with n rows and m columns.

Input format
Input one line, which contains two integers n and m, which respectively represent the number of rows and columns of the graphics you want to output.

Output format
Output n lines, each m characters, for your graphics.

Sample input
5 7

Sample output

ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC

Data scale and convention

1 <= n, m <= 26

Source: http://lx.lanqiao.cn/problem.page?gpid=T7

2. Thinking analysis:

① Analyzing the question, we can know that the first is to output the letters corresponding to the current line in reverse order until the letter A, and then output the remaining letters in the current line. The main idea is correct, but some test cases have not passed, mainly because the line is not considered. The number may be greater than the number of columns, so at this time, it is not possible to output in reverse order at the beginning until the letter A. We need to determine at the beginning whether the number of rows is greater than the number of columns. If the number of rows is greater than the number of columns Count, then you need to set a variable to count the number of output letters in the current line. The purpose of counting is to control the number of output letters to be less than or equal to the number of columns.

② Because the python language is used, the chr and ord functions are used to convert between letters and asci values. The chr function converts the ascii value to the corresponding letter, and the ord function can obtain the ascii value corresponding to the current letter

3. The code is as follows:

if __name__ == '__main__':
    n, m = map(int, input().split())
    for i in range(n):
        # 行数大于了列数的时候时候那么就需要控制每行输出字母的个数
        if n > m:
            j, count = i, 0
            while j >= 0 and count < m:
                print(chr(j + ord("A")), end="")
                j -= 1
                count += 1
        else:
            for j in range(i, -1, -1):
                print(chr(j + ord("A")), end="")
        for j in range(1, m - i):
            print(chr(j + ord("A")), end="")
        print()

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/115317682