贪心算法:
例如:当你去卖柠檬水 5块钱一杯
你一开始没有零钱
你需要给每位顾客合理的找钱
这时候要判断能不能找钱就需用贪心算法
程序如下:
s = [5] a = [0,0,0] true = 1 for i in range(len(s)): if s[0] != 5: print(False) true = 0 break elif s[i] == 5: a[0] = a[0] + 1 elif s[i] == 10: if a[0] >= 1: a[1] = a[1] + 1 a[0] = a[0] - 1 else: print(False) true = 0 break elif s[i] == 20: if a[0] >= 1 and a[1] >= 1: a[2] = a[2] + 1 a[1] = a[1] - 1 a[0] = a[0] - 1 elif a[0] >= 3: a[2] = a[2] + 1 a[0] = a[0] - 3 else: print(False) true = 0 break if true == 1: print("true")