第一题:36进制转10进制(不能使用转换函数,用例中出现非36进制需要返回0,有范围限制)
def f(num):
dic = {'a':10,'b':11,'c':12,'d':13,'e':14,'f':15,'g':16,
'h':17,'i':18,'j':19,'k':20,'l':21,'m':22,'n':23,
'o':24,'p':25,'q':26,'r':27,'s':28,'t':29,'u':30,
'v':31,'w':32,'x':33,'y':34,'z':35}
re = 0
if not num[0].isdigit() and num[0]!='+' and num[0]!='-' and not dic.get(num[0]):
return 0
tmp = 1
if num[0] == '+':
tmp = 1
num = num[1:]
if num[0] == '-':
tmp = -1
num = num[1:]
n = len(num)
j = 0
for i in range(n-1,-1,-1):
if num[i].isdigit():
re += 36**j*int(num[i])
else:
if not dic.get(num[i]):
return 0
re += 36**j*dic.get(num[i])
j += 1
return re*tmp
s = input().strip()
re = f(s)
if re>9223372036854775807:
re = 9223372036854775807
if re<-9223372036854775807:
re = -9223372036854775807
print(re)
第二题:连续n个正整数最多可以构造多少个不同形状的二叉搜索树
n = int(input().strip())
if n==0:
print(0)
else:
dp = [0]*(n+1)
dp[0]=1
dp[1]=1
for i in range(2,n+1):
for j in range(1,i+1):
dp[i]+=dp[j-1]*dp[i-j]
print(dp[-1])
全部评论
(0) 回帖