我的任务是'编写一个函数selectCoins,要求用户输入金额(以便士为单位),然后输出每个面额的硬币数(从2英镑到1英镑),用于弥补该金额确切地说(使用尽可能少的硬币).例如,如果输入为292,则该函数应报告:1×£2,0×£1,1×50p,2×20p,0×10p,0×5p,1×2p,0×1p.(提示:使用整数除法和余数).
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'))
while True:
if money >= twopound:
mOney= money - twopound
a = a + 1
elif money >= onepound:
mOney= money - onepound
b = b + 1
elif money >= fiftyp:
mOney= money - fiftyp
c = c + 1
elif money >= twentyp:
mOney= money - twentyp
d = d + 1
elif money >= tenp:
mOney= money - tenp
e = e + 1
elif money >= fivep:
mOney= money - fivep
f = f + 1
elif money >= twop:
mOney= money - twop
g = g + 1
elif money >= onep:
mOney= money - onep
h = h + 1
else:
mOney= 0
break
print(a,b,c,d,e,f,g,h)
我是编程的新手,所以当我运行这个代码时,它只输入'1 0 0 0 0 0 0 0'当我输入292而不是它应该输出的内容.
1> Barranka..:
由于您不熟悉编码,因此您应该开始编写您在纸上所遵循的程序,然后找出可以使用哪些工具来自动执行此过程.
重要
按顺序阅读完整的答案!
不要因为立即阅读代码而感到沮丧.
我提供的解决方案是隐藏的,但您可以阅读它们将鼠标悬停在它们上面或单击它们(如果您使用的是StackExchange移动应用程序,请触摸每个块中的"扰流板"链接).
算法
我要做的是:
假设我有带硬币的垃圾箱,每个垃圾箱都标有硬币面额.
垃圾箱从最大面额到最低面额排序,我总是从最高面额的垃圾箱中挑选尽可能多的硬币,然后再转移到下一个垃圾箱.
在一张纸上写下我需要计算每种面额所需硬币数量的价值.
从第一个bin(具有最高面额的bin)开始.
从那个垃圾箱中挑选我需要的硬币,这样我就不会"超过"写在纸上的金额(注意这个数字可以为零).
这可以用整数除法完成 ; 例如,如果您的值为700且bin具有面额200,则计算整数除法700 ÷ 200 = 3 (plus a remainder of 100)
计算我挑选的硬币总量.
敲击步骤5中计算的值,并将余数写为"新"值.
由于您已经在步骤4中计算了整数除法,因此可以计算余数.您还可以考虑在大多数编程语言中都有一个"Modulo"运算符,它将立即为您提供整数除法的余数.使用上述示例,700 mod 200 = 100
其中读取"700模200为100"或"整数除法700÷200的余数为100".
转到下一个硬币箱.
从步骤4开始重复,直到我使用所有箱子或值为零.
例
假设我从一个值开始,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.
工具箱
假设你使用的是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]
让我们将每个步骤的结果存储在第二个列表中:
coins = [] # We'll use the '.append()' method to add elements to this list
所以,从第一个"bin"开始:
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)
正如我的一位朋友所说:"快速,准确,简洁;不缓慢,分散和混乱"
TL; DR
在Python 3.x中,"整数除法"运算符是//
,余数(模数)运算符是%
.
您可以在一行代码中执行多个分配:
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提供了很大的灵活性.随意尝试不同的方式来获得你需要的东西......并选择更容易的方法.
希望这可以帮助.