前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【PAT甲级】Come on! Let's C

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

作者头像
喜欢ctrl的cxk
发布2019-11-08 14:21:06
3210
发布2019-11-08 14:21:06
举报
文章被收录于专栏:Don的成长史Don的成长史

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

代码语言:txt
复制
                 本文链接:[https://blog.csdn.net/weixin\_42449444/article/details/89447710](https://blog.csdn.net/weixin_42449444/article/details/89447710) 

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:

代码语言:javascript
复制
6
1111
6666
8888
1234
5555
0001
6
8888
0001
1111
2222
8888
2222

Sample Output:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
  • 解题思路:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档