PAT 甲级1116 Come on! Let's C (20分)

趣味编程竞赛奖品分配
浙江大学计算机科学与技术学院举办的“Let’sC”编程竞赛,采用独特的奖品分配规则,冠军获神秘大奖,排名为素数者得小黄人,其余人巧克力。本篇解析竞赛规则,分享AC代码实现。

“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?
题目大概意思是把名次做分类还要标记这个名次是否查询过
AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=100001;
int first;//第一名编号
vector<int>prime,Other;//计算已有的质数编号和除去第一名的编号和查询编号列表
bool vis[maxn];
bool is_prime(int s){
if(s==2||s==3) return true;
for(int i=2;i<=sqrt(s);i++){
if(s%i==0) return false;}
return true;}
int main(){
fill(vis,vis+maxn,false);
int n,k,data;
scanf("%d",&n);
scanf("%d",&first);
for(int i=2;i<=n;i++){
scanf("%d",&data);
if(is_prime(i))  prime.push_back(data);
else Other.push_back(data);}
scanf("%d",&k);
for(int i=1;i<=k;i++){
    scanf("%d",&data);
if(vis[data])
printf("%04d: Checked\n",data);
else if(data==first){
        printf("%04d: Mystery Award\n",data);
        vis[data]=true;}
else if(find(prime.begin(),prime.end(),data)!=prime.end()){
    printf("%04d: Minion\n",data);
vis[data]=true;}
else if(find(Other.begin(),Other.end(),data)!=Other.end()){
    printf("%04d: Chocolate\n",data);
vis[data]=true;}
else  printf("%04d: Are you kidding?\n",data);}
system("pause");
return 0;}
### 关于 PAT 甲级 1116 题目的 Java 实现 对于PAT甲级1116题,虽然存在偏好使用C或C++的声音[^2],但依然可以采用Java来解决此问题。以下是针对该题的一种可能的Java实现方式。 #### 使用哈希表记录节点信息 考虑到树结构的特点,在处理过程中利用HashMap存储结点及其子节点的信息能够有效简化操作流程并提高效率。具体来说: - 创建两个`Map<Integer, List<Integer>>`别用于保存左孩子和右孩子的映射关系。 - 输入数据时更新上述两张表单;遍历完成后即可轻松获取任意指定编号结点的孩子列表。 ```java import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Map<Integer, Integer> parentMap = new HashMap<>(); Map<Integer, List<Integer>> childrenMap = new HashMap<>(); while (scanner.hasNext()) { int nodeCount = scanner.nextInt(); if(nodeCount == 0){ break; } for(int i = 0 ;i<nodeCount;i++){ int id = scanner.nextInt(); String relation = scanner.next(); if ("L".equals(relation)) { // Left child parentMap.put(id, scanner.nextInt()); childrenMap.computeIfAbsent(parentMap.get(id), k -> new ArrayList<>()).add(id); } else if ("R".equals(relation)){// Right child parentMap.put(scanner.nextInt(),id ); childrenMap.computeIfAbsent(id,k->new ArrayList<>()); } } solve(childrenMap,parentMap); } } private static void solve(Map<Integer,List<Integer>> childrenMap ,Map<Integer,Integer>parentMap ){ Set<Integer> allNodes = new HashSet<>(childrenMap.keySet()); allNodes.addAll(parentMap.values()); long internalNodeCount=allNodes.stream().filter(n-> !n.equals(-1)&&!childrenMap.containsKey(n)).count(); System.out.println(internalNodeCount+" "+(allNodes.size()-internalNodeCount)); } } ``` 这段代码实现了读取输入,并构建了一个表示家族树的数据结构。最后计算内部节点数量以及叶子节点的数量并输出结果[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值