The second provincial match of the 11th Blue Bridge Cup Python Group-Serpentine Filling the Numbers

1. Problem description:

As shown in the figure below, Xiaoming fills an infinite matrix with a "snake shape" of positive integers starting from 1.
1 2 6 7 15…
3 5 8 14…
4 9 13…
10 12…
11
…… It is
easy to see that the number in the second row and second column of the matrix is ​​5. Could you please calculate the number in the 20th row and 20th column of the matrix? This is a fill-in-the-blank question, you only need to calculate the result and submit it. The result of this question is an integer. Only fill in this integer when submitting the answer, and fill in the extra content will not be scored.

2. Thinking analysis:

Analyzing the question, we can know that we can judge the rules of these numbers when filling in according to the characteristics of the numbers in the question. We can find that there are two repetitive directions of generating actions starting from the number 2. The first direction is lower left-lower, and the first direction is lower left-lower. The two directions are upper right-right. It can be known that these two repetitive directions can be represented by two while loops, that is, the two directions are represented by loops. The first loop termination condition is reached when going down left After the first column, you need to go down one row at this time. The termination condition of the second loop is that the first row is reached when going up. At this time, you need to go one row to the right, and the analysis problem can know because it is the 20th row. And the 20th column, so the answer is definitely not generated on the boundary, so it must be generated when filling in the numbers in the two loops, so we should set a flag f at the beginning so that we find when we go through the two loops If you reach the 20th row and 20th column, then all the loops should be exited at this time, so the outermost loop is needed to nest two loops for filling numbers, combined with a flag f to end all loops. And we can declare a two-dimensional list to record the result of the corresponding position in the two-dimensional list when filling in the numbers, and finally output this list to determine whether the algorithm has errors (this method is still more effective to check whether the code has errors)

3. The code is as follows:

if __name__ == '__main__':
    # 首先从数字2开始进行循环, 左下-下, 右上-右这两个方向分别为一组, 主要还是找规律吧, 找出循环的共性条件即可
    x, y, n = 0, 1, 2
    # 设置一个标志来是否找到了这个数字了
    f = 1
    # 其实可以声明一个列表进行验证看是否生成的数字的位置正确
    li = [[0] * 100 for i in range(100)]
    li[0][0], li[0][1] = 1, 2
    while f:
        # 注意y大于0才循环因为在循环中做出了减1的动作
        while y > 0:
            # 判断是否是找到了, 很明显答案只能够在矩阵的中间范围, 不可能是在边界上得到范围所以在这里判断即可
            if x == 19 and y == 19:
                f = 0
                break
            y -= 1
            x += 1
            # 数字加1
            n += 1
            li[x][y] = n
        # 往下走一行, 注意必须是当没有找到这个数字的时候才可以执行往下或者是往右走一格
        if f:
            x += 1
            n += 1
            li[x][y] = n
        while x > 0:
            # 判断是否是找到了
            if x == 19 and y == 19:
                f = 0
                break
            x -= 1
            y += 1
            n += 1
            li[x][y] = n
        # 往右边走一格
        if f:
            y += 1
            n += 1
            li[x][y] = n
    print(n)
    for i in range(100):
        for j in range(100):
            print(li[i][j], end=" ")
        print()

 

Guess you like

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