规格: Ubuntu 13.04,Python 3.3.1
一般背景: Python初学者;
特定问题背景:我已经筋疲力尽,试图解决这个问题,而且我知道,除了学习Python的教学价值之外,这个问题很无聊,也没有任何方式让这个世界变得更美好:-(所以如果你能就这个令人筋疲力尽的问题分享一些指导,我会更加感激。但如果你对这类问题不感兴趣,我真的不想浪费你的时间。< / p>
我打算做什么:“计算价值低于1美元的基本美国硬币的数量。一分钱价值1美分,镍价值5美分,一分钱一分钱10美分, 一季度价值25美分。 1美元需要100美分。因此,如果金额少于1美元(如果使用浮动,转换为此练习的整数),则计算实现金额所需的每种硬币的数量,最大化较大面额硬币的数量。例如,给定0.76美元或76美分,正确的输出将是“3季度和1便士”。 “76便士”和“2季度,2角钱,1镍和1便士”等产出是不可接受的。“
我能想出的是什么:
penny = 1
nickel = 5
dime = 10
quarter = 25
i = input("Please enter an amount no more than 1 dollar(in cents): ")
i = int(i)
if i > 100:
print ("Please enter an amount equal or less than 100. ")
elif i >= quarter:
quarter_n = i % quarter
i = i - quarter * quarter_n
if i >= dime:
dime_n = i % dime
i = i - dime * dime_n
if i >= nickel:
nickel_n = i % nickel
i = i - nickel * nickel_n
if i >= penny:
penny_n = i % penny
print (quarter_n,"quarters,",dime_n,"dimes",nickel_n,"nickels",penny_n,"pennies")
else:
if i >= penny:
penny_n = i % penny
print (quarter_n,"quarters,",dime_n,"dimes",penny_n,"pennies")
else:
if i >= nickel:
nickel_n = i % nickel
i = i - nickel * nickel_n
if i >= penny:
penny_n = i % penny
print (quarter_n,"quarters,",nickel_n,"nickels",penny_n,"pennies")
else:
if i >= penny:
penny_n = i % penny
print (quarter_n,"quarters,",penny_n,"pennies")
else:
if i >= dime:
dime_n = i % dime
i = i - dime * dime_n
if i >= nickel:
nickel_n = i % nickel
i = i - nickel * nickel_n
if i >= penny:
penny_n = i % penny
print (dime_n,"dimes",nickel_n,"nickels",penny_n,"pennies")
else:
if i >= penny:
penny_n = i % penny
print (dime_n,"dimes",penny_n,"pennies")
else:
if i >= nickel:
nickel_n = i % nickel
i = i - nickel * nickel_n
if i >= penny:
penny_n = i % penny
print (nickel_n,"nickels",penny_n,"pennies")
else:
if i >= penny:
penny_n = i % penny
print (penny_n,"pennies")
这个解决方案虽然是我能想到的最好的解决方案,但在输入实际输入数字时却无法正常工作。我无法弄清楚原因。此外,我知道即使从代码的庞大大小出现问题也是如此。我搜索了类似的问题,但我得到的最接近的是处理非常困难的数学,我无法理解。
我的问题:我知道我不能要求一个完整的解决方案,因为这取决于我解决它。我会感激a)一般指针在正确的思路上b)批评我当前的代码/思路,以便我可以改进它。
感谢您抽出宝贵时间,即使只是阅读本文!
答案 0 :(得分:4)
我认为如果你为所有mod运算符%
执行“查找和替换”,切换整数除//
,你的解决方案实际上可能正常工作。
假设您有76
美分,并希望找到季度数。使用76 % 25
会产生1
,而76 // 25
会产生3
。
关于代码,您可能应该考虑迭代可能的硬币值而不是巨大的if
,elif
混乱。
尝试这样的事情。可能需要解释的唯一部分是使用divmod
,但它实际上只是整数除法的tuple
,模数结果。您可以使用它分别获得硬币数量和新金额。
def coins_given(amount):
coins = [(25, 'quarter'), (10, 'dime'), (5, 'nickel'), (1, 'penny')]
answer = {}
for coin_value, coin_name in coins:
if amount >= coin_value:
number_coin, amount = divmod(amount, coin_value)
answer[coin_name] = number_coin
return answer
print coins_given(76)
# {'quarter': 3, 'penny': 1}
答案 1 :(得分:1)
我认为你的算法过于复杂, 你不需要所有的elifs和elses 只需检查,如果然后修改剩余的金额,直到你达到零
类似这样的事情
penny = 1
nickel = 5
dime = 10
quarter = 25
q = 0
d = 0
n = 0
p = 0
i = input("Please enter an amount no more than 1 dollar(in cents): ")
i = int(i)
if i>=25:
q = i/quarter
i %= quarter
if i>=10:
d = i/dime
i%=dime
if i>=5:
n = i/nickel
i %= nickel
if i>0:
p = i/penny
i = 0
print "The coins are %i quarters, %i dimes, %i nickels and %i pennys." %(q , d, n, p)
>>>
Please enter an amount no more than 1 dollar(in cents): 99
The coins are 3 quarters, 2 dimes, 0 nickels and 4 pennys.
>>>
Please enter an amount no more than 1 dollar(in cents): 76
The coins are 3 quarters, 0 dimes, 0 nickels and 1 pennys.
答案 2 :(得分:0)
dime=10
nickel=5
penny=1
quarter=25
def change(cents):
changeIs = ""
qs = cents/quarter
cents %= quarter
if qs > 0 :
changeIs += str(qs) + " quarter(s)"
ds = cents/dime
cents %= dime
if ds > 0 :
changeIs += " " + str(ds) + " dime(s)"
ns = cents/nickel
cents %= nickel
if ns > 0 :
changeIs += " " + str(ns) + " nickel(s)"
if cents > 0 :
changeIs += " " + str(cents) + " cent(s)"
return changeIs
if __name__ == '__main__':
cents=int(raw_input("Enter the change: "))
print change(cents)