【PAT甲级 - C++题解】1124 Raffle for Weibo Followers

简介: 【PAT甲级 - C++题解】1124 Raffle for Weibo Followers

1124 Raffle for Weibo Followers


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.

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
• 1
• 2
• 3

Sample Output 2:

Keep going...


思路

先将所有信息用 name 来保存,方便查找。并且可以用哈希表来存储中奖者的信息,排除重复中奖的情况。

从第一位中奖者位置 s 开始查找,但是要注意当前查找位置 k 不能超过总信息数 m 。

如果当前中奖者之前已经中过奖,需要往后延一位。反之,输出该中奖者信息,然后将其信息放入哈希表中,并寻找下一位中奖者。

如果哈希表为空,则说明没有中奖者,输出 Keep going... 。

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
string name[N];
int m, n, s;
int main()
{
    cin >> m >> n >> s;
    for (int i = 1; i <= m; i++)   cin >> name[i];
    unordered_set<string> hash; //用于判断重复中奖者
    int k = s;    //从第s个位置开始
    while (k <= m)
    {
        if (hash.count(name[k])) k++;    //如果该人中过奖,则往后延
        else
        {
            cout << name[k] << endl;
            hash.insert(name[k]);   //加入哈希表中
            k += n;   //找下一个中奖者
        }
    }
    //如果hash表为空,说明没有中奖者
    if (hash.empty())    puts("Keep going...");
    return 0;
}


目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
66 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
82 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
77 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
76 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
79 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
80 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
136 0
|
1天前
|
存储 编译器 C语言
【c++丨STL】string类的使用
本文介绍了C++中`string`类的基本概念及其主要接口。`string`类在C++标准库中扮演着重要角色,它提供了比C语言中字符串处理函数更丰富、安全和便捷的功能。文章详细讲解了`string`类的构造函数、赋值运算符、容量管理接口、元素访问及遍历方法、字符串修改操作、字符串运算接口、常量成员和非成员函数等内容。通过实例演示了如何使用这些接口进行字符串的创建、修改、查找和比较等操作,帮助读者更好地理解和掌握`string`类的应用。
13 2
|
7天前
|
存储 编译器 C++
【c++】类和对象(下)(取地址运算符重载、深究构造函数、类型转换、static修饰成员、友元、内部类、匿名对象)
本文介绍了C++中类和对象的高级特性,包括取地址运算符重载、构造函数的初始化列表、类型转换、static修饰成员、友元、内部类及匿名对象等内容。文章详细解释了每个概念的使用方法和注意事项,帮助读者深入了解C++面向对象编程的核心机制。
30 5
|
13天前
|
存储 编译器 C++
【c++】类和对象(中)(构造函数、析构函数、拷贝构造、赋值重载)
本文深入探讨了C++类的默认成员函数,包括构造函数、析构函数、拷贝构造函数和赋值重载。构造函数用于对象的初始化,析构函数用于对象销毁时的资源清理,拷贝构造函数用于对象的拷贝,赋值重载用于已存在对象的赋值。文章详细介绍了每个函数的特点、使用方法及注意事项,并提供了代码示例。这些默认成员函数确保了资源的正确管理和对象状态的维护。
42 4