(使用回圈或遞回),我正在嘗試撰寫一個 python 函式,用戶在其中輸入一定數量的美元(例如:1.25)和硬幣數量(例如:6),然后該函式決定它是否是假設硬幣是 25 美分 (0.25)、10 美分 (0.10)、5 美分 (0.05) 和 1 美分 (0.010),則可以使用給定的確切數量的硬幣來組成準確數量的美元。該函式可以多次使用任何一個硬幣,但使用的硬幣總數必須等于傳遞給該函式的確切數量。
例如:如果我們傳遞 1.00 美元和 6 個硬幣的數量:應該回傳 True,因為我們可以使用(3 25 美分 2 角錢 1 鎳)
1.25 美元使用 5 個硬幣:正確 >>(5 個季度)
1.25 美元使用 8 個硬幣:正確 >>(3 25 美分 5 角錢)
1.25 美元使用 7 個硬幣:錯誤。
我心中有解決方案的想法,但無法將其轉換為 python 代碼:該函式必須開始迭代我們擁有的一組硬幣(從最高硬幣開始:0.25)并將其乘以傳遞的數字. 當結果高于給定的美元數額時,傳遞的硬幣數量應減 1。當我們到達 (number * coin) 的結果小于給定的美元數額時,數量應該是(給定的數量 - (數字 * 硬幣))并且硬幣的數量應該是(給定的數量 - 目前使用的數量)。幾天來我一直在嘗試用它制作一個 python 代碼。這是我到目前為止所做的。`
def total(dollars, num):
dollars = float(dollars)
sofar = 0
num = round(num, 2)
coins = [0.25, 0.10, 0.05, 0.01]
possible = False
if not possible:
for x in range(len(coins)):
if num * coins[x] == dollars:
possible = True
elif num * coins[x] > dollars:
num -= 1
sofar = 1
else:
dollars -= num * coins[x]
num = sofar
return possible
`
- 當我將 (1.25, 5) 傳遞給函式時 >> True
- (1.25, 6) >> 錯誤
- (1.25, 7) >> 錯誤
- (1.25, 8) >> False(這是一個錯誤的回傳值)
提前致謝
uj5u.com熱心網友回復:
這是一個沒有遞回的作業解決方案,但確實使用了串列理解。不確定您的硬幣集預計會增長到多大,并且由于這會計算所有組合的總和,因此無法很好地擴展。
from itertools import combinations_with_replacement
list_of_coins = [0.1, 0.05, 0.25] # dimes, nickles, quarters
number_of_coins = 6 # as your example gives
sum_value = 1.0 # as your example gives
def will_it_sum(list_of_coins, number_of_coins, sum_value):
list_of_combos = list(combinations_with_replacement(iter(list_of_coins), number_of_coins))
summed_list = [sum(item) for item in list_of_combos]
summed_to_desired_value = [i for i in summed_list if i == sum_value]
if len(summed_to_desired_value) > 0:
return True
else:
return False
number_of_coins = 7
sum_value = 1.25
will_it_sum(list_of_coins, number_of_coins, sum_value)
uj5u.com熱心網友回復:
下面是使用 python 已經內置的解決方案。這可能不會很好地作為練習解決方案。
from itertools import combinations_with_replacement
def total(dollars, num):
cents = int(dollars*100)
coins = [25, 10, 5, 1]
return any(sum(comb) == cents for comb in combinations_with_replacement(coins, num))
或者,如果應回傳組合:
from itertools import combinations_with_replacement
def total(dollars, num):
cents = int(dollars*100)
coins = [25, 10, 5, 1]
try:
return next(filter(lambda comb: sum(comb) == cents, combinations_with_replacement(coins, num)))
except StopIteration:
return None
uj5u.com熱心網友回復:
這解決了問題而不使用combinations. 這是我上面描述的演算法。你開始使用盡可能多的最大硬幣,然后你遞回地看看你是否可以用較小的硬幣填充。一旦你得到一場比賽,你就贏了。
請注意,我使用了一個幫助程式,因此我可以將美元轉換為整數便士,以避免出現浮點問題。
coins = [25, 10, 5, 1]
def totalhelp(cents, num, which=0):
if which >= len(coins):
return False
cts = coins[which]
# What's the most of this coin we can use?
maxn = min(cents // cts, num)
for i in range(maxn,-1,-1):
amt = i * cts
if amt == cents:
return True
if totalhelp(cents-amt, num-i, which 1):
return True
return False
def total(dollars, num):
cents = int(dollars*100 0.5)
return totalhelp( cents, num )
print( total(1.25, 4 ) )
print( total(1.25, 5 ) )
print( total(1.25, 6 ) )
print( total(1.25, 7 ) )
print( total(1.25, 8 ) )
輸出:
False
True
True
True
True
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/535803.html
標籤:Python递归货币
上一篇:如何從嵌套結構中獲取子孫串列?
