洛谷P1143 进制转换的Python解法(17行)

洛谷P1143 进制转换的Python解法(17行)

洛谷P1143 进制转换
我的解法:

def to_10(num,use):#快捷的N进制->10进制的方法
    return int(num,use)
def to_n(n,x):
	#存储所有进制(1~36)的数字、字母
    a,b,c = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D','E', 'F', 'G', 'H', 'I', 'J',
             'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],[],''
    while True:#计算
        s,y = n // x,n % x
        b += [y]
        if s == 0:
            break
        n = s
    b.reverse()
    for i in b:
        c += str(a[i])
    return str(c)
#运用2个函数完成题目
use,x,to = int(input()),input().lower(),int(input())
print(to_n(to_10(x,use),to))

猜你喜欢

转载自blog.csdn.net/MRH0420/article/details/110087926