124
社区成员




贪心算法概述:每一次都要最好的结果,从而达到最终问题的结果是最好的。(全局最优解)
问题描述:一个人能吃东西的多少为胃口,记为g[i],东西的大小为尺寸,记为s[i]。满足胃口并且吃到最小的饼干,则为局部最优解。直到每个人都吃到饼干或者没有饼干即为全局最优解。
代码如下:
g = list(map(int,input().split()))#wei kou
s = list(map(int,input().split()))#chi cun
g.sort()
s.sort()
count = 0
for i in range(len(g)):
for j in range(len(s)):
if s[j]>=g[i]:
count += 1
s[j] = -1#相当于拿走这块饼干
break
print(count)