1.动态规划问题、
最小乘车费用
def process(money,lenght):
if lenght==0:
return 0
ans=999999
for i in range(min(lenght,10)):
ans=min(money[i]+process(money,lenght-i-1),ans)
return ans
money=[12,21,31,40,49,58,69,79,90,101]
print(process(money,15))
2.给出三个正整数,数与数之间可以用加减乘除任意组合,数字之间的前后顺序可以调换,请打印出这三个数是否能组合运算得到24,如果能,返回true,如果不能,返回false
def process(x,y):
return set([x+y,x-y,x*y,x/y,y-x,y/x])
def processTrue(x,y,z):
if process(24,x)&process(y,z):
return True
if process(24,y)&process(x,z):
return True
if process(24,z)&process(x,y):
return True
return False
print(processTrue(1,1,1))
3.给定一个字符串,统计其中的不重复的英文单词的数量
4.给定一个数列如:ids=[1,1,1,2,2,3],从数列中随机取出m个数,求出剩下数列中元素种类最少的值
def process(arr,n,m):
hashmap={}
helplist=[]
for i in arr:
if i not in hashmap:
hashmap[i]=1
else:
hashmap[i]+=1
ans=len(hashmap)
for x,y in hashmap.items():
helplist.append([x,y])
helplist=sorted(helplist,key=lambda z:z[1],reverse=False)
for i in range(len(helplist)):
while m>0 and helplist[i][1]>0:
helplist[i][1]-=1
m-=1
if helplist[i][1]==0:
ans-=1
if m==0:
return ans
return ans
print(helplist)
print(process([1,1,1,2,2,3],6,2))
5.输入一个范围,求出范围之内的所有水仙花数
def shuixianhua():
ans=""
while True:
arr=input().split(" ")
if arr==[""]:
return ans
m=int(arr[0])
n=int(arr[1])
res = ""
for i in range(m, n + 1):
bai = int(i / 100)
shi = int((i - bai * 100) / 10)
ge = int(i - bai * 100 - shi * 10)
if i == ge ** 3 + shi ** 3 + bai ** 3:
res = res +" "+ str(i)
if res == "":
ans=ans+"\nno"
else:
ans=ans+"\n"+res
return ans
print(shuixianhua())