John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers on Weibo -- that is, he would select winners from every N followers who forwarded his post, and give away gifts. Now you are supposed to help him generate the list of winners.
小明 PAT 考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔 N 个人就发出一个红包。请你编写程序帮助他确定中奖名单。
Input Specification:
Each input file contains one test case. For each case, the first line gives three positive integers M (≤ 1000), N and S, being the total number of forwards, the skip number of winners, and the index of the first winner (the indices start from 1). Then M lines follow, each gives the nickname (a nonempty string of no more than 20 characters, with no white space or return) of a follower who has forwarded John's post.
Note: it is possible that someone would forward more than once, but no one can win more than once. Hence if the current candidate of a winner has won before, we must skip him/her and consider the next one.
输入第一行给出三个正整数 M(≤ 1000)、N 和 S,分别是转发的总量、小明决定的中奖间隔、以及第一位中奖者的序号(编号从 1 开始)。随后 M 行,顺序给出转发微博的网友的昵称(不超过 20 个字符、不包含空格回车的非空字符串)。
注意:可能有人转发多次,但不能中奖多次。所以如果处于当前中奖位置的网友已经中过奖,则跳过他顺次取下一位。
Output Specification:
For each case, print the list of winners in the same order as in the input, each nickname occupies a line. If there is no winner yet, print Keep going... instead.
按照输入的顺序输出中奖名单,每个昵称占一行。如果没有人中奖,则输出 Keep going...。
Sample Input 1:
9 3 2
Imgonnawin!
PickMe
PickMeMeMeee
LookHere
Imgonnawin!
TryAgainAgain
TryAgainAgain
Imgonnawin!
TryAgainAgain
Sample Output 1:
PickMe
Imgonnawin!
TryAgainAgain
Sample Input 2:
2 3 5
Imgonnawin!
PickMe
Sample Output 2:
Keep going...
解题思路
- 依次读入字符串;
- 模拟抽奖过程,当抽中的人不在中奖人集合中时将其插入集合,当抽中的人在中奖人集合中时查看下一位,到达结尾时退出;
- 当退出时集合中元素数量为0时输出边界,否则按照题目要求输出;
- 返回零值。
代码
#include<iostream>
#include<cstdio>
#include<string>
#include<set>
using namespace std;
int M,N,S;
set<string> st;
int main(){
int i;
string temp;
scanf("%d%d%d",&M,&N,&S);
st.clear();
for(i=1;i<=M;i++){
cin>>temp;
if(i==S){
if(st.find(temp)!=st.end()){
S++;
}else{
cout<<temp<<endl;
st.insert(temp);
S+=N;
}
}
}
if(st.size()==0){
cout<<"Keep going..."<<endl;
}
return 0;
}
运行结果