输出L内所有数字的乘积末尾0的个数

思路:将10分解为2*5,注意这里只能分解为2*5,所以我们可以将列表L内的数字分解为因数2和5的个数。

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)


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值