将一个数字分成更小的特定数字

我想创建一个程序,它接受用户的输入并返回账单中的值

即如果输入是 110,我想编程输出:

1 x 100
1 x 10

如果输入是87我想编程输出

4 x 20
1 x 5
2 x 1

等等。有人知道该怎么做吗?


米脂
浏览 87回答 2
2回答

慕村9548890

您可以使用整数除法来获取每张钞票适合的频率。bills = [20, 5, 1]input = 87for bill in bills:    integer_div = input // bill    if integer_div > 0:        print(f'{integer_div} x {bill}')        input -= integer_div * bill结果4 x 201 x 52 x 1

陪伴而非守候

def change(amount, bills):    money = {}    for bill in bills:        bill_count = amount/bill        money[bill] = bill_count        amount -= bill * bill_count    return moneyresult = change(87, [20, 5, 1])for coin, amount in result.items():    if amount != 0:        print("%d X %d" % (amount, coin))将得到所需的结果。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python