题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
- 取出输入的第二字符到尾字符
- 将字符挨个存储list中
- 建立非数字字符的对应值字典
- 遍历list进行计算
value_input = input().strip()[2:]
value_list = []
for i in range(len(value_input)):
value_list.append(value_input[i])
value_dict = {'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15}
count = len(value_list)
value = 0
for i in range(len(value_list)):
if value_list[i] not in value_dict:
value += int(value_list[i]) * 16 ** (count - 1)
else:
value += value_dict[value_list[i]] * 16 ** (count - 1)
count -= 1
print(value)