博客
关于我
【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关于isinstance函数的解析 | 附Demo(全)
    查看>>
    Python3:setup.py:完成所有任务的PIP安装(Build_ext+Install)
    查看>>
    python4.0 gil_Python 并发编程系列--1、浅谈GIL历史及对多线程的影响
    查看>>
    python60行绘图程序_天底下最简单的QT画图板,就一个类,60行代码
    查看>>
    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 的子进程调用不起作用
    查看>>