博客
关于我
【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 在包含类似字符\x16、\x12、\x某某的数组中将以\x开头的字符找出来的方法
    查看>>
    Python 在并行进程之间共享字典
    查看>>
    Python 垃圾收集器文档
    查看>>
    python 基于 wordcloud + jieba + matplotlib 生成词云
    查看>>
    python 基于detectron或mask_rcnn的mask遮罩区域进行图片截取
    查看>>
    Python 基础 - Day 5 Learning Note - 模块 之 标准库:time (1)
    查看>>
    python 基础第七篇
    查看>>
    Python编程:Tkinter图形界面设计(1)
    查看>>
    Python 基础语法:None
    查看>>
    Python 基础语法:基本数据类型(一)
    查看>>
    python编程读取写入excel_Python读取txt内容写入xls格式excel中的方法
    查看>>
    Python 基础语法:基本数据类型(字典)
    查看>>
    Python 基础语法:基本数据类型(字符串)
    查看>>
    Python 基础语法:基本数据类型(集合)
    查看>>
    Python编程的终极十大工具
    查看>>
    python 堆排序
    查看>>
    python编程方式之一-函数式编程
    查看>>
    python 复习计划
    查看>>
    Python 多处理 apply_async 永远不会在 Windows 7 上返回结果
    查看>>