PAT 1069 微博转发抽奖

1069 微博转发抽奖(20 分)
小明 PAT 考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔 N 个人就发出一个红包。请你编写程序帮助他确定中奖名单。
输入格式:
输入第一行给出三个正整数 M( 1000 )、N 和 S,分别是转发的总量、小明决定的中奖间隔、以及第一位中奖者的序号(编号从 1 开始)。随后 M 行,顺序给出转发微博的网友的昵称(不超过 20 个字符、不包含空格回车的非空字符串)。
注意:可能有人转发多次,但不能中奖次。所以如果处于当前中奖位置的网友已经中过奖,则跳过他顺次取下一位。
输出格式:
按照输入的顺序输出中奖名单,每个昵称占一行。如果没有人中奖,则输出 Keep going…。
输入样例 1:

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

输出样例 1:

PickMe
Imgonnawin!
TryAgainAgain

输入样例 2:

2 3 5
Imgonnawin!
PickMe

输出样例 2:

Keep going...




解析

排队的翻版。
queue保存数据。把抽过的人放在set里。
每隔N个人抽奖,就是从队列里pop N个人。当这个人从来没有中奖,也就是set里不存在。就打印出来,并放在set里。
如果这个人中过奖,就要接着pop,直到pop的人不在set里。(每次pop的时候要考虑queue的size()哦,别pop空队列)。


我还是把 Keep going…放在了const string。防止我打错了o( ̄▽ ̄)o

     const string out="Keep going...";

code:

#include<bits/stdc++.h>
using namespace std;
const string out="Keep going...";
int main()
{
    int M,N,S;
    cin>>M>>N>>S;
    queue<string> data;
    string temp;
    for(int i=0;i<M;i++){
        cin>>temp;
        data.push(temp);
    }
    if(M<S)
        cout<<out;
    else{
        set<string> get;
        for(int i=0;i<S;i++){
            temp=data.front();
            data.pop();
        }   
        get.insert(temp);
        cout<<temp<<endl;
        while(data.size() >=N){
            for(int i=0;i<N;i++){
                temp=data.front();
                data.pop();
            }
            while(data.size()>=1 && get.find(temp) !=get.end()){
                temp=data.front();
                data.pop();
            }
            if(get.find(temp) ==get.end()){
                get.insert(temp);
                cout<<temp<<endl;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41256413/article/details/82145628
今日推荐