目录

【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页。
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
cherryc_19 分钟前
JavaSE基础——第六章 类与对象(二)
java·开发语言
代码的乐趣19 分钟前
支持selenium的chrome driver更新到135.0.7049.42
chrome·python·selenium
SsummerC3 小时前
【leetcode100】数组中的第K个最大元素
python·算法·leetcode
伊玛目的门徒3 小时前
解决backtrader框架下日志ValueError: I/O operation on closed file.报错(jupyternotebook)
python·backtrader·量化·日志管理·回测
java1234_小锋4 小时前
一周学会Pandas2 Python数据处理与分析-编写Pandas2 HelloWord项目
python·pandas·python数据分析·pandas2
Qlittleboy4 小时前
windows如何安装wkhtmltoimage 给PHP使用根据HTML生成图片
开发语言·windows·php
凯强同学5 小时前
第十四届蓝桥杯大赛软件赛省赛Python 大学 C 组:7.翻转
python·算法·蓝桥杯
一个真正のman.5 小时前
c加加学习之day01
学习
蔗理苦5 小时前
2025-04-03 Latex学习1——本地配置Latex + VScode环境
ide·vscode·学习·latex
水w5 小时前
【Android Studio】解决报错问题Algorithm HmacPBESHA256 not available
android·开发语言·android studio