PAT A1116 Come on! Let‘s C (20分)

前言

传送门

正文


参考题解

#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<map> 
using namespace std;
/* 题意: 给出n个参赛者的排名列表。随后进行k次查询,每次查询输入一个id,若该id不在排名列表中 则输出"Are you kidding?",若该id在之前的查询中已经查询过,则输出id:Checked,否根据该id的排名, 输出对应的奖项。 思路: 使用map<string,pair<int,bool>> 来保存每个用户的排名以及是否第一次被查询 */
map<string,pair<int,bool>> mp;
bool isPrime(int n){
	if(n<=1)return false;
	for(int i=2,j=(int)sqrt(n*1.0);i<=j;i++){
		if(n%i==0)return false;
	}
	return true;
} 
int main(){
	int n,k;
	string str;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>str;
		mp[str].first=i;
		mp[str].second=false; 
	}
	cin>>k;
	while(k--){
		cin>>str;
		auto it=mp.find(str);//查找该str 
		if(it==mp.end()){//没查到 
			printf("%s: Are you kidding?\n",str.c_str());
		}else{
			if(it->first==str){
				if(it->second.second==false){//首次查询str 
					if(it->second.first==1){//第一名 
						printf("%s: Mystery Award\n",str.c_str());
					}else if(isPrime(it->second.first)){//排名是素数 
						printf("%s: Minion\n",str.c_str());
					}else{//其他 
						printf("%s: Chocolate\n",str.c_str());
					}	
					it->second.second=true; 
				}else{
					printf("%s: Checked\n",str.c_str());
				} 	
			}
		} 
	}
	return 0;
} 
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-10 15:24
高考前一晚在OPPO手机上设置了早上5:30的闹钟,然而闹钟并未按时响起。直到妈妈做好早餐后,在6:27打开手机才发现闹钟未触发,“气得早上饭都没吃”。资本家你赢了
永不遗忘:我来解释一下 :Oppo 手机晚上两点会自动进行系统更新,这个系统更新会重置掉所有设置好的闹钟,而且他也不会告诉你,而且只有 Oppo 会这样,华为苹果小米三星都不会
点赞 评论 收藏
分享
嵌入式求职之路:可以看我经验😂,https://www.nowcoder.com/share/jump/73221730841876945
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务