#【贪心算法】找零钱,使得零钱张数最少。
def change():
MONEY = (100,50,20,10,5,1,0.5,0.1)
count = 0
x = float(input('Please enter a number:'))
for c in MONEY:
temp = 0
while x>=c:
x = x-c+1e-3 #末尾加上0.001,是为了补上损失的精度。
count += 1
temp += 1
print('{:>3}元:{}张'.format(c,temp))
print('零钱总计%d张'%count)
change()
'''
运行结果:
Please enter a number:12.3
100元:0张
50元:0张
20元:0张
10元:1张
5元:0张
1元:2张
0.5元:0张
0.1元:3张
零钱总计6张
'''