【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页。
相关推荐
Mr YiRan3 小时前
C++面向对象继承与操作符重载
开发语言·c++·算法
Emotional。3 小时前
2025 年度技术总结与规划:AI 时代的开发者成长之路
人工智能·python·ai·langchain
一只鹿鹿鹿5 小时前
智慧水利一体化建设方案
大数据·运维·开发语言·数据库·物联网
witAI6 小时前
**AI仿真人剧制作软件2025推荐,解锁沉浸式数字内容创作
人工智能·python
没有医保李先生7 小时前
字节对齐的总结
java·开发语言
Elastic 中国社区官方博客7 小时前
使用 Elastic 进行网络监控:统一网络可观测性
大数据·开发语言·网络·人工智能·elasticsearch·搜索引擎·全文检索
Codefengfeng7 小时前
Python Base环境中加包的方法
开发语言·python
清水白石0087 小时前
《Python 编程全景解析:从核心精要到测试替身(Test Doubles)五大武器的实战淬炼》
开发语言·python
如若1238 小时前
AutoDL云服务器 NVIDIA 570驱动 EGL渲染修复全记录
运维·服务器·python
甲枫叶8 小时前
【claude】Claude Code正式引入Git Worktree原生支持:Agent全面实现并行独立工作
java·人工智能·git·python·ai编程