题解:P10999 [蓝桥杯 2024 省 Python B] 穿越时空之门

· · 题解

思路

十进制转二进制有内置函数 bin,直接 str(bin(x))[2 : ] 就可以了。

十进制转四进制可以分成两步:十进制转二进制,再转成四进制。第一步已经知道怎么做了,第二步也很简单,4=2^2,所以每两位合并起来,不足补 0

00:0 01:1 10:2 11:3

代码

trans = {"00" : "0", "01" : "1", "10" : "2", "11" : "3"}
def tbin(x):
    b = str(bin(x))[2 : ]
    res = 0
    for i in range(len(b)):
        res += int(b[i])
    return res;
def tquan(x):
    b = str(bin(x))[2 : ]
    if len(b) % 2 == 1:
        b = "0" + b
    q = ""
    for i in range(0, len(b), 2):
        q += trans[b[i] + b[i + 1]]
    res = 0
    for i in range(len(q)):
        res += int(q[i])
    return res;
cnt = 0
for i in range(1, 2025):
    if tbin(i) == tquan(i):
        cnt += 1
print(cnt)

答案是 63

符合要求勇者的力量值分别是:1, 4, 5, 16, 17, 20, 21, 64, 65, 68, 69, 80, 81, 84, 85, 256, 257, 260, 261, 272, 273, 276, 277, 320, 321, 324, 325, 336, 337, 340, 341, 1024, 1025, 1028, 1029, 1040, 1041, 1044, 1045, 1088, 1089, 1092, 1093, 1104, 1105, 1108, 1109, 1280, 1281, 1284, 1285, 1296, 1297, 1300, 1301, 1344, 1345, 1348, 1349, 1360, 1361, 1364, 1365有兴趣的可以算一下