python的取余算法原理_python.取余算法

#-*- coding:utf-8-*-

'''

Created on 2012-12-28

取余。取一个任意小于1 美元的金额,然后计算可以换成最少多少枚硬币。硬币有1

美分,5 美分,10 美分,25 美分四种。1 美元等于100 美分。举例来说,0.76 美元换算结果

应该是 3 枚25 美分,1 枚1 美分。类似76 枚1 美分,2 枚25 美分+2 枚10 美分+1 枚5 美分+1

枚1 美分这样的结果都是不符合要求的

@author : admin ''' dollar = 100 dol = int(raw_input('enter a num 0 < cent < 100: ')) #先对大数25美分进行取余运算 if dol // 25 > 0:          #得到25的商数     print '25美分的硬币有',dol//25          #用余数再对10进行地板除,计算10美分的硬币有几枚     if (dol % 25) // 10 > 0:         print '10美分的硬币有',(dol % 25) // 10,'枚'         dol5 = dol % 25 % 10 // 5         if ((dol % 25) % 10) // 5 > 0:             print '5美分的硬币有',dol % 25 % 10 // 5,'枚'             print '1美分的硬币有',(dol % 25) % 10 % 5,'枚'         else:             print '1美分的硬币有',(dol % 25) % 10 % 5,'枚'     #判断如果(dol % 25) // 10 < 0的时候,继续找5美分的和1美分的硬币         else:         print '10美分的硬币有0枚'         if dol % 25 // 5 > 0:             print '5美分的硬币有',dol % 25 // 5,'枚'             print '1美分的硬币有',dol % 25 % 5,'枚'         else:             print '1美分的硬币有',dol % 25,'枚' else:     if (dol % 25) // 10 > 0:         print '没有25美分的硬币'         print '10美分的硬币有',(dol % 25) // 10,'枚'         if (dol % 25) % 10 // 5 > 0:             print  '5美分的硬币有',dol % 25 % 10 // 5,'枚'             print '1美分的硬币有',(dol % 25) % 10 % 5,'枚'         else:             print '1美分的硬币有',(dol % 25) % 10 % 5,'枚'     else:         if ((dol % 25) % 10) // 5 > 0:             print '5美分的硬币有',(dol % 25) % 10 // 5,'枚'             print '1美分的硬币有',(dol % 25) % 10 % 5,'枚'          else:             print '1美分的硬币有',(dol % 25) % 10 % 5,'枚'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值