# 解决(Python)

``````#!/usr/bin/env python
#coding:utf-8

def change_coin(money):
coin = [1,2,5,10,20,50,100]     #1分，2分，5分，1角，2角，5角，1元
coin.sort(reverse=True)
money = money*100               #以分为单位进行计算
change = {}

for one in coin:
num_coin = money//one       #除法，取整，得到该单位硬币的个数
if num_coin>0:
change[one]=num_coin
num_remain = money%one      #取余数，得到剩下的钱数
if num_remain==0:
break
else:
money = num_remain
return change

if __name__=="__main__":
money = 3.42
num_coin = change_coin(money)
result = [(key,num_coin[key]) for key in sorted(num_coin.keys())]
print "You have %s RMB"%money
print "I had to change you:"
print "    Coin    Number"
for i in result:
if i[0]==100:
print "Yuan    %d    %d"%(i[0]/100,i[1])
elif i[0]<10:
print "Fen    %d    %d"%(i[0],i[1])
else:
print "Jiao    %d    %d"%(i[0]/10,i[1])

#执行结果
#You have 3.42 RMB
#    Coin    Number
#    Fen    2    1
#    Jiao    2    2
#    Yuan    1    3
``````

# 解决2(Python)

``````def coinChange(centsNeeded, coinValues):
minCoins = [[0 for j in range(centsNeeded + 1)] for i in range(len(coinValues))]
minCoins[0] = range(centsNeeded + 1)

for i in range(1,len(coinValues)):
for j in range(0, centsNeeded + 1):
if j < coinValues[i]:
minCoins[i][j] = minCoins[i-1][j]
else:
minCoins[i][j] = min(minCoins[i-1][j], 1 + minCoins[i][j-coinValues[i]])
return minCoins[-1][-1]
``````