雇佣问题
一.
1 #根据给定的应聘者列表,得出最好的应聘者 2 def hire_assistant(assList): 3 n = len(assList) 4 best = 0 5 index = 0 6 for i in range(n): 7 value = assList[i].score 8 if value > best: 9 best = value 10 index = i 11 return assList[index] 12 13 # 定义一个应聘者的类,给出他的属性score和name 14 class Assistant: 15 def __init__(self, a_name="anonymous", value=0): 16 self.name = a_name 17 self.score = value 18 19 assList=[Assistant("A",12),Assistant("B",13),Assistant("C",8),Assistant("D",10),Assistant("E",22),Assistant("F",21)] 20 print(hire_assistant(assList).name) 21 ------------------------------------------------- 22 E
二.
1 #平衡最小化面试次数和最大化应聘者质量 2 def on_line_maximum(k,assList): 3 n = len(assList) 4 bestscore = float("-inf")#最好的分数 5 #先搜寻前k个中分数最高的 6 for i in range(k): 7 if assList[i].score > bestscore: 8 bestscore = assList[i].score 9 #对于后面的,如果有更高分数的,立即选择,否则选择最后一个 10 for i in range(k,n): 11 if assList[i].score > bestscore: 12 return assList[i] 13 return assList[n-1] 14 15 assList=[Assistant("A",12),Assistant("B",13),Assistant("C",8),Assistant("D",10),Assistant("E",22),Assistant("F",21)] 16 assistant = on_line_maximum(3, assList) 17 print(assistant.name) 18 ---------------------------------- 19 E