博客
关于我
C++/Python PAT乙级1069 微博转发抽奖 (20分)
阅读量:633 次
发布时间:2019-03-14

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

为了解决这个问题,我们需要编写一个程序来帮助小明确定微博转发抽奖活动的中奖名单。程序需要从转发的网友中按固定间隔选出中奖者,并确保每个网友只能中奖一次。

方法思路

  • 读取输入数据:首先读取转发总量M、中奖间隔N以及第一位中奖者的序号S。
  • 存储转发名单:将转发的网友昵称存储在一个列表中。
  • 跟踪中奖者:使用一个集合来记录已经中奖的网友,避免重复中奖。
  • 确定中奖名单:从指定的序号开始,每隔N个位置检查一个网友是否已经中奖。如果没有中奖过,则将其标记为中奖者并加入结果列表。
  • 输出结果:根据结果列表输出中奖名单。如果没有中奖者,则输出“Keep going...”。
  • 解决代码

    m, n, s = map(int, input().split())names = [input().strip() for _ in range(m)]result = []used = set()i = s - 1  # 转换为0索引while i < m:    current = names[i]    if current not in used:        used.add(current)        result.append(current)    i += nif result:    for name in result:        print(name)else:    print("Keep going...")

    代码解释

  • 读取输入:使用input().split()读取M、N和S的值,然后读取接下来的M行昵称。
  • 初始化变量result列表用于存储中奖名单,used集合用于记录已中奖的网友。
  • 遍历名单:从序号S开始(转换为0索引),每隔N个位置检查一个网友。如果该网友未中奖,则标记并加入结果。
  • 输出结果:如果有中奖名单,逐行输出;否则输出“Keep going...”。
  • 这个方法确保我们正确地从指定位置开始,每隔固定间隔选出中奖者,并避免重复中奖,满足题目的所有要求。

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

    你可能感兴趣的文章
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘html-webpack-plugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>