博客
关于我
HDU 2089:不要62
阅读量:248 次
发布时间:2019-02-28

本文共 877 字,大约阅读时间需要 2 分钟。

为了解决这个问题,我们需要计算在给定的区间 [n, m] 内,不含有不吉利数字的数字的数量。不吉利数字定义为包含数字 4 或连续出现 62 的数字。

方法思路

我们可以使用暴力枚举的方法来解决这个问题。具体步骤如下:

  • 对于每个数字 x,在区间 [n, m] 内,检查其是否包含不吉利数字 4 或 62。
  • 如果数字不包含这些不吉利数字,则计数器加一。
  • 最终输出计数器的结果。
  • 这种方法的时间复杂度是 O((m - n + 1) * len(str(m))),其中 len(str(m)) 是数字的位数。由于题目给出的 m 的最大值为 10^6,这种方法在 Python 中是可行的。

    解决代码

    import sysdef count_valid(n, m):    count = 0    for x in range(n, m + 1):        s = str(x)        if '4' in s or '62' in s:            continue        count += 1    return countn, m = map(int, sys.stdin.readline().split())while True:    line = sys.stdin.readline()    if not line:        break    n, m = map(int, line.strip().split())    if n == 0 and m == 0:        break    print(count_valid(n, m))

    代码解释

  • 读取输入:使用 sys.stdin.readline 读取输入,直到遇到两个连续的 0 时结束循环。
  • 计数函数 count_valid:遍历区间 [n, m] 内的每个数字,检查其是否包含 4 或 62。如果不包含,则计数器加一。
  • 输出结果:对于每个输入的区间,输出符合条件的数字数量。
  • 这种方法直接且简洁,能够在给定的约束条件下高效地解决问题。

    转载地址:http://gubp.baihongyu.com/

    你可能感兴趣的文章
    Python: AES加密与解密
    查看>>
    Python: Range() 最大尺寸;动态的还是静态的?
    查看>>
    Python: Windows下用 multiprocessing 的深坑
    查看>>
    python:Could not find platform independent libraries <prefix>解决方法
    查看>>
    Python:datetime tzinfo 时区名称文档
    查看>>
    Python:deepcopy(list) vs new_list = old_list[:]
    查看>>
    Python:Google API - 从消息中获取 mimeTypes
    查看>>
    Python:Google API - 从消息中获取 mimeTypes
    查看>>
    Python:subprocess.call,stdout to file,stderr to file,在屏幕上实时显示stderr
    查看>>
    Python:TypeError:无法连接“str“和“int“对象
    查看>>
    Python:XPath 在 ElementTree 中不可用
    查看>>
    Python:从 FTP 服务器下载文件
    查看>>
    python:从列表中创建元组列表
    查看>>
    Python:从嵌套列表中删除重复项
    查看>>
    Python:从间隔映射到值
    查看>>
    Python:使用 shell=False 的子进程调用不起作用
    查看>>
    Python:修改 PATH 环境变量的平台无关方式
    查看>>
    Python:假与 0
    查看>>
    Python:函数使用
    查看>>
    Python:函数进阶
    查看>>