题目描述:
输入一个整数和进制,转换成十进制输出
输入格式:
在一行输入整数和进制
输出格式:
在一行十进制输出结果
输入样例:
在这里给出一组输入。例如:
45,8
输出样例:
在这里给出相应的输出。例如:
37
题解
a,b = input().split(",")
b = int(b)
print(int(a,b))
做了这个题才发现python的强大好用,而且int函数居然还可以这么玩,真的是惊到我了。查看python源代码了解int函数的使用,
源代码注释如下:
def __init__(self, x, base=10): # known special case of int.__init__
"""
int([x]) -> integer
int(x, base=10) -> integer
Convert a number or string to an integer, or return 0 if no arguments
are given. If x is a number, return x.__int__(). For floating point
numbers, this truncates towards zero.
If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base. The literal can be preceded by '+' or '-' and be surrounded
by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
# (copied from class doc)
"""
pass
注意这一段话:
If x is not a number or if base is given, then x must be a string,bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-’ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
大概意思如下:(本人英语水平有限,大概意思了解就行)
如果x不是一个数字,或者如果给定了base值,那么x必须是整数字面值的base进制的一个一个字符串、字节或者字节数组实例。x前面可以“+”或“-”(即x可以是正负),并且被空格包围。base默认为10。base范围是0和2-36。0表示将base进制的字符串解释为十进制整数。
注释也给了个例子:
int(‘0b100’, base=0)
结果是4
即base为0时候,将标准的其他进制的整数(字符串型)转换为对应的十进制整数。
人生苦短,我学Python!