The third simulation game of the 12th Blue Bridge Cup-ranked 2021

1. Problem description:

In Excel, the column names from column 1 to column 26 are from A to Z. Starting from column 27, the column names are composed of two letters. The column names from column 27 to column 702 are from AA to ZZ. The following columns are represented by 3 letters and 4 letters. Excuse me, what is the column name for column 2021? 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 problem, we can know that it is actually a base conversion problem. We can regard it as a conversion from decimal to 26. This is similar to the conversion of decimal to other bases. The method is also to divide the current base. To get the remainder, so we have to divide by 26 each time, and then concatenate the letters corresponding to the remainder in turn. At the beginning, we can declare that a list with a length of 26 corresponds to 26 letters. When we get the current remainder from the list Just take out the current letter in the middle. The second method is pure hand calculation, because the calculation amount is not particularly large, so direct calculation is also possible.

3. The code is as follows:

if __name__ == '__main__':
    remainder = list()
    # 生成列表中的内容
    for i in range(26):
        remainder.append(chr(65 + i))
    # 输出测试结果看生成的结果是否正确
    print(remainder)
    n = 2021
    res = ""
    while n != 0:
        # 对应索引的位置需要减去1
        res = remainder[n % 26 - 1] + res
        n //= 26
    print(res)
if __name__ == '__main__':
    print(2021 // (26 * 26))
    print(2021 % (26 * 26))
    # 第二位为Y
    print(669 // 26)
    print(669 % 26)

    # 第一位为S
    for i in range(19):
        if i == 18: print(chr(i + 65))
    # 所以最终三个数字为: "BYS"

 

Guess you like

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