【PAT甲级】Come on! Let's C

Problem Description:

"Let's C" is a popular and fun programming contest hosted by the College of Computer Science and Technology, Zhejiang University. Since the idea of the contest is for fun, the award rules are funny as the following:

  • 0、 The Champion will receive a "Mystery Award" (such as a BIG collection of students' research papers...).
  • 1、 Those who ranked as a prime number will receive the best award -- the Minions (小黄人)!
  • 2、 Everyone else will receive chocolates.

Given the final ranklist and a sequence of contestant ID's, you are supposed to tell the corresponding awards.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​4​​), the total number of contestants. Then N lines of the ranklist follow, each in order gives a contestant's ID (a 4-digit number). After the ranklist, there is a positive integer K followed by K query ID's.

Output Specification:

For each query, print in a line ID: award where the award is Mystery Award, or Minion, or Chocolate. If the ID is not in the ranklist, print Are you kidding?instead. If the ID has been checked before, print ID: Checked.

Sample Input:

6
1111
6666
8888
1234
5555
0001
6
8888
0001
1111
2222
8888
2222

Sample Output:

8888: Minion
0001: Chocolate
1111: Mystery Award
2222: Are you kidding?
8888: Checked
2222: Are you kidding?

解题思路:

这道题PAT乙级里面出现过:【PAT乙级】C语言竞赛,我当时写的代码有点丑陋,参赛者ID是string型。首先自定义函数isPrime是肯定得有的,然后建立第一个map,map的key和value分别存放ID和排名,完成ID和排名的输入之后,再建立第二个map,map的key和value分别存放ID和是否领取了奖品(0为未领取,1为已领取)。接着是发放奖品,按题意来就行了,①排名第一的赢得一份“神秘大奖”;②排名为素数的学生将赢得最好的奖品 —— 小黄人玩偶;③其他人将得到巧克力。若奖品已被领取就输出"Checked";④如果输入的ID没有再排名里面,就输出"Are you kidding?",你在逗我吗? 

AC代码: 

#include <bits/stdc++.h>
using namespace std;

bool isPrime(int n)  //判断是不是素数
{
    if(n < 2)
    {
        return false;
    }
    for(int i = 2; i <= sqrt(n); i++)
    {
        if(n%i == 0)
        {
            return false;
        }
    }
    return true;
}

int main()
{
    int N;   //N个参赛者
    cin >> N;   
    map<int,int> m;  //map的key用来存放参赛者ID,value用来存放参赛者的排名   
    for(int i = 1; i <= N; i++)  //根据排名来输入参赛者ID
    {
        int temp;
        cin >> temp;
        m[temp] = i;
    }
    int K;   //K个待查询的ID
    cin >> K;
    set<int> s;  //用来记录领过奖的参赛者
    for(int i = 0; i < K; i++)
    {
        int temp;   
        cin >> temp;
        if(m[temp] == 1)  //冠军将收到"Mystery Award"
        {
            if(s.count(temp) == 0)  //若没有领取过奖品
            {
                printf("%04d: Mystery Award\n", temp);
                s.insert(temp); 
            }
            else  //若已经领取过奖品啦
            {
                printf("%04d: Checked\n", temp);
            }
        }
        else if(isPrime(m[temp]))  //排名是素数的参赛者将收到"Minion"
        {
            if(s.count(temp) == 0)   //若没有领取过奖品
            {
                printf("%04d: Minion\n", temp);
                s.insert(temp);
            }
            else    //若已经领取过奖品啦
            {
                printf("%04d: Checked\n", temp);
            }
        }
        else if(m[temp] != 0)  //其余参赛者领取巧克力
        {
            if(s.count(temp) == 0)   //若没有领取过奖品
            {
                printf("%04d: Chocolate\n", temp);
                s.insert(temp);
            }
            else    //若已经领取过奖品啦
            {
                printf("%04d: Checked\n", temp);
            }
        }
        else   //没有参赛的人来领奖将输出"Are you kidding?"
        {
            printf("%04d: Are you kidding?\n", temp);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42449444/article/details/89447710