作业求助
Q1). Write a program that begins by reading a number of cents from the user as an integer. Then your program should compute and display the denominations of the coins that should be used to give that amount of change to the shopper. The change should be given using as few coins as possible. Assume that the machine is loaded with pennies, nickels, dimes, quarters, loonies and tooniesPennies一分,Nickels五分,dime 一角,quarters两毛五,loonies 一块,toonies两块求求大神帮忙解答。选修课把我整没了,感谢。。如果能给出答案再加个详解最好。万分感谢。双膝跪地
这是例子
#Sanjay
#999999999
#
#Example adding two integers < your Lab or assignment number>
# PsuedoCode Begin
#
# Obtain two numbers from the user
# Add the numbers
# Display the result
#
# PsuedoCode End
# Input
temp = input('Enter Your Number1: ')
number1 = int(temp)
temp = input('Enter Your Number2: ')
number2 = int(temp)
# Can do this too
# number = int(input("Enter a number: "))
# Processing
result= number1 + number2
# Output
print( "The result of adding " + str(number1)+ " to " + \
str(number2) + " is equal to " + str(result)) https://blackboard.uwindsor.ca/bbcswebdav/pid-1653216-dt-content-rid-23567480_1/courses/COMP2067-91-R-2021S/Lab3.pdf 这是需要得到的结果 def denominations(c: 'cents') -> str: # 面值兌換函數,輸出str格式
coins = [('Pennies', 1), ('Nickels', 5), ('Dime', 10), ('Quarters', 25), ('Loonies', 100), ('Toonies', 200)] # 創建對照面值列表
coins.sort(reverse = True, key = lambda x: x) # 由大至小排序
res = [] # 定義新列表,用於輸出結果
for coin, value in coins: # coin 表示面值名稱, value 表示值
if c >= value: # 如果現有的硬幣足夠兌現面值大的 coin,就兌現
x, y = divmod(c, value) # x 表示商 y 表示餘數
res.append(f"{x} {coin}") # 儲存已兌現 coin
c = y # 剩餘硬幣,重複執行兌現,直至硬幣完全兌現為止
return ', '.join(res) # 最終將列表裡的所有結果合成
cents = int(input())
print(denominations(cents))
輸入/輸出結果:
1234
6 Toonies, 1 Quarters, 1 Nickels, 4 Pennies
页:
[1]