输出L内所有数字的乘积末尾0的个数
2014-02-26 10:07
274 查看
思路:将10分解为2*5,注意这里只能分解为2*5,所以我们可以将列表L内的数字分解为因数2和5的个数。
python非递归代码
python递归版本
python非递归代码
def zerocount(L): count_2 = 0 count_5 = 0 #calculate count 2 and 5 for i in L: temp = i while temp%2 == 0: temp = temp /2 count_2 += 1 temp = i while temp%5 == 0: temp = temp / 5 count_5 += 1 print count_2,count_5 #get small count small = count_2 if small > count_5: small = count_5 return small L = [2,8,3,50] print zerocount(L)
python递归版本
import os,sys def divisorNum(value,divisor): if value % divisor != 0: return 0 else: return 1+divisorNum(value/divisor,divisor) def zerocount(L): count_2 = 0 count_5 = 0 #calculate count 2 and 5 for i in L: count_2 += divisorNum(i,2) count_5 += divisorNum(i,5) print count_2,count_5 #get small count small = count_2 if small > count_5: small = count_5 return small L = [2,8,3,50] print zerocount(L)
相关文章推荐
- 常见的几种RuntimeException
- GLFW 简单入门学习
- ADO连接字符串总结
- 解决 Ubuntu 12.04 无法调节屏幕亮度的问题
- AIX 常用命令汇总 erikxue(薛忠权)
- IIS断开连接之后internet信息服务里面不显示本地计算机的解决方法
- Android环境问题集锦之十九:红色感叹号与No projects are found to import
- QTP11使用DOM XPath以及CSS识别元素对象
- sharepoint2010企业序列号
- IIS断开连接之后internet信息服务里面不显示本地计算机的解决方法
- 为什么提议设置南京大屠杀公祭日
- mysql_free_result&bool mysqli_close
- ios--常用元素控件尺寸
- [转]myeclipse 生成JAR包并引入第三方包
- 来更新一篇blog吧
- lync2010安装失败、安装必备组件失败:SqlExpressRtc
- AIX 安装 RAC 无法发现节点(后遗症。。。)
- Ubuntu下PDF文件转JPG图片
- JS获取select的value和text值的简单实例
- poj 1837 DP