美文网首页
统计指定数字总数的Python实现

统计指定数字总数的Python实现

作者: 辛未 | 来源:发表于2019-04-12 00:50 被阅读0次

功能说明

统计指定范围[0, n]内所有的奇数数字中,3的数量总和。

实现代码

#!/usr/bin/python

def calcCountOfDigit3InBase(base):
    if base == 10: return 1
    return 10 * calcCountOfDigit3InBase(base / 10) + (base / 20)

def calcCountOfDigit3(n):
    base = 10
    while n / base > 0:
        base *= 10
    base /= 10

    count = 0
    while base >= 10:
        m = n / base
        count += m * calcCountOfDigit3InBase(base)
        if m > 3:
            count += base / 2
        if m == 3:
            count += ((n + 1) % base) / 2

        n = n % base
        base /= 10

    if n >= 3:
        count += 1
    return count

def printCountOfDigit3(n):
    print("%s = %s" % (n, calcCountOfDigit3(n)))

输出结果

printCountOfDigit3(800000000)
printCountOfDigit3( 60000000)
printCountOfDigit3(  6000000)
printCountOfDigit3(   200000)
printCountOfDigit3(    70000)
printCountOfDigit3(     8000)
printCountOfDigit3(      100)
printCountOfDigit3(       70)
printCountOfDigit3(        1)
printCountOfDigit3(866278171)

seewin@seewin:~$ python StatsCountOfDigit3.py 
800000000 = 410000000
60000000 = 29000000
6000000 = 2600000
200000 = 60000
70000 = 22500
8000 = 2100
100 = 15
70 = 12
1 = 0
866278171 = 441684627

不知结论正确与否,欢迎批评指正。

相关文章

  • 统计指定数字总数的Python实现

    功能说明 统计指定范围[0, n]内所有的奇数数字中,3的数量总和。 实现代码 输出结果 不知结论正确与否,欢迎批...

  • 27.Remove Element

    移除指定元素,返回剩下元素的总数。 注意点: 函数传入的是实参,所以必须要把指定元素移除,不能只是单纯统计数量。 ...

  • Python获取数组中指定内容出现的次数

    Python第一个数字阵列 Python获取数组中指定内容出现的次数 Python获取数组中指定内容的位置 Pyt...

  • 找出数组中的幸运数

    题目: 题目的理解: 统计数组中相同数字的个数,然后获取个数最多的。 python实现 想看最优解法移步此处 提交...

  • django分组统计

    1、统计每个月的订单总数 2、统计每年的订单总数 3、统计每件商品卖出去的订单总额(分组显示) 4、统计所有订单的...

  • 3个python操控微信的操作,统计好友性别、自动回复、生成词云

    三个案例: python实现统计好友性别比 以及统计个性签名,生成词云 python实现简单的自动回复 当然在学习...

  • 统计学--感知机

    参考李航的统计学习 感知机学习算法 Python实现感知机代码 Python代码实现对偶形式

  • 查看linux文件目录的大小

    查看linux文件目录的大小 统计总数大小 du -sh统计所在文件夹总数大小du -h | grep G查看...

  • 统计语法

    1.统计总数count(id) 2.sum group by统计 PHP语句 3.查询0<余额<50的会员人数和余额总数

  • jq 计算统计总数

    $(function(){ vartotal=0; vartotals=0; vark=

网友评论

      本文标题:统计指定数字总数的Python实现

      本文链接:https://www.haomeiwen.com/subject/qtofwqtx.html