PAT (Advanced Level) Practice 1124 Raffle for Weibo Followers (20 分) 凌宸1642

PAT (Advanced Level) Practice 1124 Raffle for Weibo Followers (20 分) 凌宸1642

题目描述:

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 行,每行给出转发 John 帖子的关注者的昵称(不超过 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 Input1 (样例输入1):

9 3 2
Imgonnawin!
PickMe
PickMeMeMeee
LookHere
Imgonnawin!
TryAgainAgain
TryAgainAgain
Imgonnawin!
TryAgainAgain

Sample Output1 (样例输出1):

PickMe
Imgonnawin!
TryAgainAgain

Sample Input2 (样例输入2):

2 3 5
Imgonnawin!
PickMe

Sample Output2 (样例输出2):

Keep going...

The Idea:

不需要存储所有字符串,但是需要存储已经获奖的人的姓名,所以需要一个记录获奖人姓名的 map,然后根据输入第几个时,判断是否中奖即可。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
map<string , bool> mp ;
int m , n , s , cnt ;
string str ;
int main(){
	cin >> m >> n >> s ;
	for(int i = 1 ; i <= m ; i ++){
		cin >> str ;
		if(i == s) {
			if(mp.count(str) == 0) {  // 没中过奖 
				cout << str << endl ;
				mp[str] = true ;  // 标记已经中过奖
				s = s + n ; // 下一个中奖位置就是隔 n 个之后
				cnt ++ ;
			} else s = s + 1 ;	//已经中过奖了,往下顺移一位
		}
	}
	if(cnt == 0) cout << "Keep going..." << endl ;
	return 0 ;
}  
posted @ 2021-08-15 13:58  凌宸1642  阅读(37)  评论(0编辑  收藏  举报