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

  • 2019 年 11 月 8 日
  • 筆記

版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接: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:

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 Awardn", temp);                  s.insert(temp);              }              else  //若已經領取過獎品啦              {                  printf("%04d: Checkedn", temp);              }          }          else if(isPrime(m[temp]))  //排名是素數的參賽者將收到"Minion"          {              if(s.count(temp) == 0)   //若沒有領取過獎品              {                  printf("%04d: Minionn", temp);                  s.insert(temp);              }              else    //若已經領取過獎品啦              {                  printf("%04d: Checkedn", temp);              }          }          else if(m[temp] != 0)  //其餘參賽者領取巧克力          {              if(s.count(temp) == 0)   //若沒有領取過獎品              {                  printf("%04d: Chocolaten", temp);                  s.insert(temp);              }              else    //若已經領取過獎品啦              {                  printf("%04d: Checkedn", temp);              }          }          else   //沒有參賽的人來領獎將輸出"Are you kidding?"          {              printf("%04d: Are you kidding?n", temp);          }      }      return 0;  }