我的任务是 “编写一个功能selectCoins,要求用户输入金额 (便士),然后输出每种面额的硬币数量(从2英镑起 到1p)应该用来精确地补足该金额(使用尽可能少的 硬币数量)。例如,如果输入为292,则该函数应报告: 1×£2、0×£1、1×50p,2×20p,0×10p,0×5p,1×2p,0×1p。 (提示:使用整数 除法和余数)。'
h
我是编程新手,因此在运行此代码时,它只是输入 当我键入292而不是它应输出的内容时,则为“ 1 0 0 0 0 0 0 0 0”。
答案 0 :(得分:5)
由于您是编码的新手,因此应该开始编写在纸上遵循的过程,然后找出可以使用哪些工具来自动化该过程。
重要
按顺序阅读完整答案!
不要迷上马上阅读代码的诱惑。我提供的解决方案是隐藏的,但是您可以将鼠标悬停在它们上面或单击它们(如果您使用的是StackExchange移动应用,请触摸每个块中的“扰流板”链接)以阅读它们。
我要做的是:
700 ÷ 200 = 3 (plus a remainder of 100)
700 mod 200 = 100
读为“ 700模200为100”或“整数除法700÷200的余数为100”。假设我从292
的值开始,并且我有以下面额(已经从最高面额到最低面额排序)的纸箱:
| 200 | 100 | 50 | 20 | 10 | 5 | 2 | 1 |
+------+------+------+------+------+------+------+------+
| I | II | III | IV | V | VI | VII | VIII |
所以,让我们看看如果应用上述算法会发生什么:
Write the value: 292
Start with the first bin (denomination: 200)
Pick 1 coin from the bin
The total amount picked from the bin is 200
The remainder is 92
Strike the previous value
The new value is 92
Move to the next bin (denomination: 100)
Pick 0 coins from the bin
The total amount picked from the bin is 0
The remainder is 92
Strike the previous value
The new value is 92
Move to the next bin (denomination: 50)
Pick 1 coin from the bin
The total amount picked from the bin is 50
The remainder is 42
Move to the next bin (denomination: 20)
Pick 2 coins from the bin
The total amount picked from the bin is 20
The remainder is 2
Move to the next bin (denomination: 10)
Pick 0 coins from the bin
The total amount picked from the bin is 0
The remainder is 2
Move to the next bin (denomination: 10)
Pick 0 coin from the bin
The total amount picked from the bin is 0
The remainder is 2
Move to the next bin (denomination: 5)
Pick 0 coin from the bin
The total amount picked from the bin is 0
The remainder is 2
Move to the next bin (denomination: 2)
Pick 1 coin from the bin
The total amount picked from the bin is 2
The remainder is 0
Done
Python是一种非常清晰的语言,使此类任务变得容易。因此,让我们尝试将算法转换为Python。
假设您使用的是Python 3.x,则需要了解一些运算符:
//
):如果仅用一个斜杠除,则将得到“实数除法”(例如3 / 2 == 1.5
),但是如果您使用双斜杠,则会得到“整数除法(例如3 // 2 = 1
)%
):如上所述,该运算符返回除法的余数(例如7 % 4 == 3
)这些运算符一起使用,将为您提供每一步所需的信息:
292 // 200 == 2
292 % 200 == 92
92 // 100 == 0
92 % 100 == 92
...
Python的一个有用特性是您可以执行“多次赋值”:您可以在一个步骤中将多个值赋给多个变量:
# Initialize the value:
value = 292
# Initialize the denomination:
denomination = 200
# Calculate the amount of coins needed for the specified denomination
# and get the remainder (overwriting the value), in one single step:
coins, value = value // denomination, value % denomination
# ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
# | The remainder
# The number of coins
# (using integer division)
有了这些知识,我们可以编写解决方案:
记住:,请先阅读以上所有内容,然后再揭示以下解决方案。
此解决方案同时使用整数除法和余数来执行计算。def selectCoins(): twopound = 200 onepound = 100 fiftyp = 50 twentyp = 20 tenp = 10 fivep = 5 twop = 2 onep = 1 a = 0 b = 0 c = 0 d = 0 e = 0 f = 0 g = 0 h = 0 money = int(input('Enter how much money you have in pence')) # Example: 292 # Calculate the number of coins needed and the remainder # The remainder will "overwrite" the value previously held in the "money" variable a, money = money // twopound, money % twopound # a = 1, money = 92 b, money = money // onepound, money % onepound # b = 0, money = 92 c, money = money // fiftyp, money % fiftyp # c = 1, money = 42 d, money = money // twentyp, money % twentyp # d = 2, money = 2 e, money = money // tenp, money % tenp # e = 0, money = 2 f, money = money // fivep, money % fivep # f = 0, money = 2 g, money = money // twop, money % twop # g = 1, money = 0 e, money = money // onep, money % onep # e = 0, money = 0 print(a,b,c,d,e,f,g,h)
面对现实:上面的代码是冗长的。一定有更好的方法……而且有!使用循环。 考虑一下算法:重复执行从一个容器到下一个容器的步骤,并获取所需的硬币数量和剩余的硬币数量。这可以写成一个循环。 因此,让我们在工具箱中添加一个
list
:然后将每个步骤的结果存储在第二个列表中:denominations = [200, 100, 50, 20, 10, 5, 2, 1]
因此,从第一个“ bin”开始:coins = [] # We'll use the '.append()' method to add elements to this list
让我们将其循环:n, money = money // denominations[0] , money % denominations[0] coins.append(n)
就是这样!def select_coins_v2(): denominations = [200, 100, 50, 20, 10, 5, 2, 1] coins = [] money = int(input('Enter how much money you have in pence')) for i in range(len(denominations)): n, money = money // denominations[i], money % denominations[i] coins.append(n) print(coins)
请注意,上面的代码仍然存在问题:您两次阅读
denominations
。如果面额值只能读取一次,那就太好了。 当然,有一种方法:正如我的一个朋友所说:“快速,准确,简洁;不要慢,漫不经心和混乱”def select_coins_v3(): denominations = [200, 100, 50, 20, 10, 5, 2, 1] coins = [] money = int(input('Enter how much money you have in pence')) for d in denominations: # 'd' will hold the value of the denomination n, money = money // d, money % d coins.append(n) print(coins)
//
,其余(模)运算符为%
。a, b = 1, 2
denominations = [200, 100, 50, 20, 10, 5, 2, 1]
n, money = money // denominations[0], money % denominations[0]
for d in denominations: n, money = money // d, money % d
如果我想同时打印面额和使用的每种面额的硬币数量怎么办?您可以使用循环遍历两个列表,但是也可以使用字典来使其简单:
def select_coins_v4(): denominations = [200, 100, 50, 20, 10, 5, 2, 1] coins = [] money = int(input('Enter how much money you have in pence')) for d in denominations: # 'd' will hold the value of the denomination n, money = money // d, money % d coins.append(n) number_of_coins = dict(zip(denominations, coins)) print(number_of_coins)
Python提供了很大的灵活性。随意尝试各种获得所需内容的方法...,然后选择简单的方法。
希望这会有所帮助。
答案 1 :(得分:1)
使用实际面额的最酷的事情是贪婪的解决方案将始终找到最佳解决方案...这对于怪异的面额不再适用...但是如果将它们分成几部分,这些问题总是最简单的
def get_one_change(amt_due):
# find how many of the largest denomination that you can use is
# ie for 60 = 1x50p is the count and number of largest
# for 4 = 4x1p ; 21 = 2x10p ; etc
return pence,count # ie 50,1 would be 1 50p coin
一旦有了这个,您只需要反复调用它并调整结果,直到没有应有的更改为止
def get_change(amount_due):
changes_due = [] # to keep track
while amount_due:
pence,qty_of_coin = get_one_change(amount_due)
changes_due.append({"coin":pence,"qty":qty_of_coin})
amount_due = amount_due - (pence*qty_of_coin)
return changes_due
现在您可以通过用户输入来调用get_change
方法