投测开岗位,两道编程
1.一问ac
def divingBoard(a, b, k): if k ==0: return [] return list(sorted(set([a * i + b * (k - i) for i in range(k+1)])))2.二问没有当场做出
input = "HEAD`0`A,B,C|A`20`END|B`100`END|C`50`D,E|D`80`F|E`3`END|F`30`END" dict = {} name_val = {} for index,trees in enumerate(input.split('|')): name, val, childs = trees.split('`') dict[name] = childs name_val[name] = int(val) def maxVal(key): if dict[key]=="END": return name_val[key] arrs = [] for i in dict[key].split(','): y = maxVal(i) if maxVal(i) != None else 0 arrs.append(name_val[key] + y ) return max(arrs) ps = maxVal('HEAD') print(ps)
全部评论
(0) 回帖