Python换硬币

将一笔零钱换成5分、2分和1分的硬币,要求每种硬币至少有一枚。

输入格式:

输入在一行中给出待换的零钱数额x∈(8,100)。

输出格式:

要求按5分、2分和1分硬币的数量依次从大到小的顺序,输出各种换法。每行输出一种换法,格式为:“fen5:5分硬币数量, fen2:2分硬币数量, fen1:1分硬币数量, total:硬币总数量”。最后一行输出“count = 换法个数”。

输入样例:

13

输出样例:

fen5:2, fen2:1, fen1:1, total:4
fen5:1, fen2:3, fen1:2, total:6
fen5:1, fen2:2, fen1:4, total:7
fen5:1, fen2:1, fen1:6, total:8
count = 4

代码示例: 

#换硬币方法
NumberTimes = 1                    #方法数
InitialMoney = int(input())        #初始输入
Remainder5 = InitialMoney % 5
Quotient5 = int(InitialMoney / 5)  #5个数
Count1 = Remainder5 % 2            #1个数
Quotient2 = int(Quotient5 / 2)     #2个数
#第一个方法
print(f'fen5:{Quotient5}, fen2:{Quotient2}, fen1:{Count1}, total:{Quotient5+Quotient2+Count1}')
#循环递减大面值币
while True:
    if Quotient5 != 1:
        Quotient5 -= 1
        Quotient2 += 2
        Count1 += 1
    elif Quotient5 == 1 and Quotient2 != 1:
        Quotient2 -= 1
        Count1 += 2
    print(f'fen5:{Quotient5}, fen2:{Quotient2}, fen1:{Count1}, total:{Quotient5 + Quotient2 + Count1}')
    NumberTimes += 1
    if Quotient5 == 1 and Quotient2 == 1:
        print(f'count = {NumberTimes}')
        break

以上代码全为本人亲自手敲,可能有一些错误和不足之处,如有更好的方法和建议,欢迎您在评论区友善讨论。  

  • 23
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

付之一笑」༻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值