PAT 1124 Raffle for Weibo Followers

约翰在微博上举办抽奖活动,从转发他帖子的每N名粉丝中选出赢家,这份包含所有获奖者昵称的完整名单揭示了他们的幸运时刻。

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.

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.

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.
在这里插入图片描述
题目大意:
确定第一个后依次确定后面的获奖者,如果该获奖者以及获过奖,就跳到下一个。
思路:

  1. 用一个map来记录获奖者名单,值为1,如果之前获过奖,就跳过,到下一个。
  2. 关于跳过到下一个这里,不能使用纯粹输入时的i来计数,应该还是需要一个量来记录,遇到第一个获奖者就变成0,开始计数,如果到下一个获奖者是已获的,则保持不变,继续遍历,直到找到下一个获奖者。
    代码:
#include<iostream>
#include<vector>
using namespace std;
vector<string> v;
bool check(string s){
	bool flag=false;
	for(int i=0;i<v.size();i++) {
		if(v[i]==s) flag=true;
	}
	return flag;
}
int main(){
	int m,n,s;
	cin>>m>>n>>s;
	int snum=0,nnum=0;
	while(m--){
		string str;
		cin>>str;
		snum++;
		if(snum==s){
			v.push_back(str);
	    	continue;
		} 
		else if(snum>s){
			if(nnum+1==n){
				if(check(str)) continue;
				else{
					v.push_back(str);
					nnum=0;
				} 
			} 
			else nnum++;
		}
	} 
	if(v.size()==0) printf("Keep going...");
	else{
		for(int i=0;i<v.size();i++) cout<<v[i]<<endl;
	}
	return 0;
}

在这里插入图片描述
因为不清楚unordered_map、unordered_set的插入顺序和遍历顺序,它返回的顺序和需要得到的顺序刚好相反,所以在最后我选取了vector来实现。
tip:

  • unordered_map、unordered_set中不能使用rbegin()和rend(); 但map和set可以使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值