1116. Come on! Let's C (20)

本文介绍了一个有趣的编程竞赛“Let's C”的奖品分配规则,并提供了一段C++代码实现,该程序根据参赛者的最终排名为其分配不同的奖品。

1116. Come on! Let's C (20)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

"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 (<=10000), 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?

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long int ll;

struct Node{
	int res;
	int flag;
	Node(){
		res = flag = 0;
	}
};
Node Rank[10005];

int prime[10005];

void Prime(){
	for(int i=0; i<=10000; i++)
		prime[i] = 1;
	prime[0] = prime[1] = 0;
	for(int i=2; i<=10000; i++){
		if(prime[i]){
			for(int j=2*i; j<=10000; j+=i)
				prime[j] = 0;
		}
	}
}


int main(){
	Prime();
	int count = 0;
	int N;
	scanf("%d",&N);
	int ID;
	for(int i=1; i<=N; i++){
		scanf("%d",&ID);
		Rank[ID].res = ++count;
	}
	int K;
	scanf("%d",&K);
	for(int i=1; i<=K; i++){
		scanf("%d",&ID);
		if(Rank[ID].res==0)
			printf("%04d: Are you kidding?\n",ID);
		else if(prime[Rank[ID].res]){
			if(Rank[ID].flag == 0){
				printf("%04d: Minion\n",ID);
				Rank[ID].flag = 1;
			}
			else
				printf("%04d: Checked\n",ID);
			}
		else if(Rank[ID].res==1){
			if(Rank[ID].flag==0){
				printf("%04d: Mystery Award\n",ID);
				Rank[ID].flag = 1;
			}
			else
				printf("%04d: Checked\n",ID);
		}
		else{
			if(Rank[ID].flag==0){
				printf("%04d: Chocolate\n",ID);
				Rank[ID].flag = 1;
			}
			else
				printf("%04d: Checked\n",ID);
		}
	}
	return 0;
}






<think>好的,用户的问题是关于PAT1116,但看起来用户可能输入有误,正确的题目编号应该是PAT甲级题目中的1116。需要确认题目的正确编号和内容。首先,PAT甲级题目1116对应的标题是“Come on! Let's C”,这是一道关于排名和奖励分配的编程问题。 接下来需要回忆或搜索该题的具体要求。根据资料,题目的大致内容是:根据参赛者的排名分配不同的奖项。第一名的获得“Mystery Award”,素数排名的获得“Minion”,其他则获得“Chocolate”。如果查询的参赛者ID不存在,显示“Checked”,如果重复查询则显示“Checked”。 解题步骤通常包括读取输入数据,处理排名,判断素数,记录已查询的ID以避免重复。需要注意的是,参赛者ID可能是字符串,而不仅仅是数字,因此处理时需注意数据类型。 关键点包括: 1. 确定排名是否为素数。素数的判断函数需要高效,因为可能有多次查询。 2. 使用哈希表或字典来存储ID对应的排名和是否已查询过。 3. 处理输出时根据不同的情况输出对应的结果。 可能的实现步骤: - 读取N个参赛者,存储ID到排名的映射。 - 读取K个查询,逐个处理: - 检查ID是否存在,不存在输出“Checked”。 - 存在则检查是否已查询过,是则输出“Checked”。 - 否则根据排名判断奖项类型,并标记为已查询。 在代码实现中,需要注意素数的判断方法,例如预先处理素数表或者每次检查时遍历到平方根。此外,参赛者ID可能需要作为字符串处理,避免前导零的问题。 现在需要将这些步骤整理成清晰的回答,确保符合用户的要求,比如分点说明、代码格式正确、不出现第一人称等。同时生成相关问题,例如关于素数判断方法、PAT甲级其他题目、如何处理重复查询等。</think>PAT甲级1116题(Come on! Let's C)要求根据参赛者排名分配奖项,并处理多次查询。以下是实现逻辑与代码示例: **核心逻辑** 1. **数据结构**:用字典存储ID与排名的映射,哈希表记录已查询状态。 2. **素数判断**:检查排名是否为质数(需处理排名=1的特殊情况)。 3. **查询处理**: - ID不存在 → 输出`Are you kidding?` - 已查询过 → 输出`Checked` - 排名1 → `Mystery Award` - 素数排名 → `Minion` - 其他 → `Chocolate` **代码实现** ```python import math def is_prime(n): if n < 2: return False for i in range(2, int(math.sqrt(n)) + 1): if n % i == 0: return False return True n = int(input()) id_rank = {} for i in range(n): id = input().strip() id_rank[id] = i + 1 # 排名从1开始 k = int(input()) checked = set() for _ in range(k): query = input().strip() if query not in id_rank: print(f"{query}: Are you kidding?") continue if query in checked: print(f"{query}: Checked") continue rank = id_rank[query] checked.add(query) if rank == 1: print(f"{query}: Mystery Award") elif is_prime(rank): print(f"{query}: Minion") else: print(f"{query}: Chocolate") ``` **边界条件处理** - 输入ID可能包含前导零(需作为字符串处理) - 排名为1时直接分配最高奖项 - 多次查询同一ID时标记检查状态 **时间复杂度** - 素数判断时间复杂度:$O(\sqrt{n})$ - 总体时间复杂度:$O(k \sqrt{n})$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值