博客
关于我
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/

    你可能感兴趣的文章
    NMF(非负矩阵分解)
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>