源代码:
d={}
keys=['zero','one','two','three','four','five','six','seven','eight','nine','ten',
'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty',
'thirty','forty','fifty','sixty','seventy','eighty','ninety']
values=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90]
for i in range(len(keys)):
d[keys[i]]=values[i]
t=eval(input())
ls1=[]
for i in range(t):
s=input()
ls1.append(s)
for j in ls1:
ls2=j.split('+')
m=0
for k in ls2:
if '-'in k:
n=k.split('-')
b=d[n[0]]+d[n[1]]
else:
b=d[k]
m=m+b
print(m)
运行结果:
心得: 这题本意就是把英文转化为相应的数字进行运算,我的做法是把相应英文与数字用字典进行映射储存,后面遍历用列表储存的英文是再从字典取值映射为相应数字即可