博客
关于我
【PAT】1124 Raffle for Weibo Followers (20 分)
阅读量:739 次
发布时间:2019-03-22

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

为了生成John的幸运观众列表,我们可以采用以下策略:

  • 读取输入数据:首先,读取N、step和first的值,然后读取每个粉丝的名字存入向量followers。

  • 初始化结构:使用vector来存储粉丝名字,并使用map来记录每个粉丝被抽中的次数,以避免重复抽选。

  • 抽奖循环:从指定的起始位置开始,每隔step个单位抽取一个粉丝。如果已经被抽中过,无需再次抽选,继续到下一个位置。

  • 更新结果:每次抽到一个新的粉丝时,将名字添加入winners向量,并将其计数加一,以便下一次抽取时跳过。

  • 处理结果:最后输出所有幸运观众的名字,或提示如果没有人中奖。

  • 以下是优化后的代码:

    #include 
    #include
    #include
    #include
    #include
    using namespace std;int main() { int n, step, first; string f; vector
    followers; vector
    winners; map
    record; scanf("%d %d %d", &n, &step, &first); for (int i = 0; i < n; i++) { cin >> f; followers.push_back(f); } int i = first - 1; // 从第first位开始 while (i < n) { int current = i; while (true) { string current_follower = followers[current]; if (record.find(current_follower) == record.end()) { break; } else { current += step; if (current >= n) { i = n; // 结束循环 break; } } } if (current >= n) break; i = current; winners.push_back(followers[current]); record[followers[current]]++; } if (winners.empty()) { cout << "Keep going...\n"; } else { for (const auto& winner : winners) { cout << winner << endl; } } return 0;}

    知识点总结:

    • set和unordered_set:set按字典序排列,unordered_set元素存储位置随机,无法保证顺序。
    • reverse()函数:用于逆转向量。
    • 效率选择:使用向量存储粉丝,通过常见结构(如map)记录抽奖次数,确保高效且避免重复抽选,不影响查询和修改性能。

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

    你可能感兴趣的文章
    Python-模块与包的管理
    查看>>
    PYTHON-模块定义 搜索路径
    查看>>
    Python-爬虫工程师-面试总结
    查看>>
    Python-类与文件读取结合
    查看>>
    python-类思想-实现简单增删查改
    查看>>
    python-网络编程
    查看>>
    python写入文件出现空行
    查看>>
    PYTHON-返回给定x的多边形路径的y坐标
    查看>>
    python-进程池与线程池,协程
    查看>>
    Python-通过函数传递变量
    查看>>
    Python内置函数详解——总结篇
    查看>>
    Python.对象集合中的标识.和散列
    查看>>
    Python.硒.拖放错误“AttributeError:move_to 需要 WebElement“
    查看>>
    Python内置函数详解
    查看>>
    Python/Kivy:从一个类调用函数到另一个类并在 Python 中显示小部件
    查看>>
    Python/Kivy:我可以在双击标签时调用函数吗
    查看>>
    python内置函数5-frozenset()
    查看>>
    Python/Plotly:如何使用要显示的信息自定义悬停模板?
    查看>>
    Python/Selenium - 无法单击 www.instagram.com 上的“接受 cookie“按钮
    查看>>
    Python内置函数(35)——next
    查看>>