微博转发抽奖
1. 题目地址
https://www.acwing.com/problem/content/1619/
2. 题目解析
这道题整体来看,比较简单。仔细阅读题意即可。需要注意的是:当第一个中奖的人的序号大于总人数的时候,一定是没有中奖者的。此时需要输出Keep going....。
判断某人是否中奖过,我们可以用一个unordered_map<string,bool>来存储。
3. 题解
不再阐述。
4. 代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <unordered_map>
using namespace std;
int m,n,s;
vector<string> persons;
unordered_map<string,bool> h;
int main(){
scanf("%d%d%d",&m,&n,&s);
for(int i = 0; i < m; i ++){
string person;
cin >> person;
persons.push_back(person);
}
for(int i = s - 1; i < persons.size();){
//如果该人中过奖
while(i < persons.size() && h[persons[i]]){
i++;
}
//如果越界
if(i >= persons.size()){
break;
}
//使得该人中奖,并输出
h[persons[i]] = true;
printf("%s\n",persons[i].c_str());
//跳到下一个中奖者
i+=n;
}
//如果第一位中奖者的序号大于总人数,那么一定没有中奖者。
if(m < s){
printf("Keep going...");
}
return 0;
}