【Python 小学低段竞赛数学题】数字5在书本页码中出现16次,这本书最多有多少页

书的页码编号是按1 2 3 4 5一直这样下去的,数字5恰好出现了16次,请问这本书最多可以有多少页?

包含5的页码: 5 15 25 35 45 50 51 52 53 54 55 56 57 58 59 65

需要注意页码55,出现了两次5。因此到59页就满足数字5出现了16次。且59页是书本最少总页数。最多总页数要往17次算出页数65,65页刚好第一次满足出现17次数字5,所在64页就是数字5出现16次的书本最多总页数。

python 复制代码
def page_num_count(specify_number: int, set_times: int) -> tuple[int, int, list[int]]:
    page_num = 1
    num_count = 0  # 记录5出现的总次数
    list_specify_number = []  # 包含指定数字的页码,组成列表。
    while True:
        count = str(page_num).count(str(specify_number))  # 统计5在当前页码中出现的次数
        if count > 0:
            num_count += count  # 当前页码有5,将次数累加进总次数中
            list_specify_number.append(page_num)
        if num_count == set_times:
            break
        page_num += 1
    return page_num, num_count,list_specify_number


page_num, num_count, list_specify_number = page_num_count(5, 16)

print(f"5出现的总次数:{num_count}")  # 5出现的总次数:16
print(f"最少:{page_num}页。")  # 最少:59页。
print(f"包含5的页码:", *list_specify_number)  # 包含5的页码: 5 15 25 35 45 50 51 52 53 54 55 56 57 58 59


page_num, num_count, list_specify_number = page_num_count(5, 17)  # 共出现16次,可能的最多页数,要按17次算,将刚满足17次的页码减1,即是16次最多页数。

print(f"按17次算,包含5的页码:", *list_specify_number)  # 按17次算,包含5的页码: 5 15 25 35 45 50 51 52 53 54 55 56 57 58 59 65
print(f"最多:{page_num-1}页。")  # 最多:64页。
相关推荐
武子康2 小时前
Java-82 深入浅出 MySQL 内部架构:服务层、存储引擎与文件系统全覆盖
java·开发语言·数据库·学习·mysql·spring·微服务
倒悬于世2 小时前
开源的语音合成大模型-Cosyvoice使用介绍
人工智能·python·语音识别
惜.己2 小时前
pytest中使用skip跳过某个函数
开发语言·python·测试工具·pytest
姜暮儿2 小时前
C++ 性能优化
开发语言·c++
啊呦.超能力3 小时前
QT开发---多线程编程
开发语言·qt
挽风8213 小时前
Excel file format cannot be determined, you must specify an engine manually.
python
铭哥的编程日记4 小时前
《从C风格到C++风格:内存管理的进化之路》
开发语言·c++
秃了也弱了。4 小时前
reflections:Java非常好用的反射工具包
java·开发语言
叫我:松哥4 小时前
基于网络爬虫的在线医疗咨询数据爬取与医疗服务分析系统,技术采用django+朴素贝叶斯算法+boostrap+echart可视化
人工智能·爬虫·python·算法·django·数据可视化·朴素贝叶斯
Joker—H5 小时前
【Java】Reflection反射(代理模式)
java·开发语言·经验分享·代理模式·idea