PAT甲级1116 Come on! Let‘s C (20分)|C++实现

一、题目描述

原题链接
在这里插入图片描述

Input Specification:

在这里插入图片描述

​​Output Specification:

在这里插入图片描述

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?

二、解题思路

题目给的ID为四位数字,我们可以用一个数组表示每个ID对应的排名,不存在则设为0,写一个判断素数的函数checkPrime,接着对每个输入的query进行判断输出即可。详情见代码注释。

三、AC代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 10010;
int ranks[maxn] = {0};  //存放每个ID对应的排名
bool checked[maxn]; //标记是否已经检查过
bool checkPrime(int n)  //判断是否为素数
{
  for(int i=2; i*i<=n; i++)
  {
    if(n%i == 0)	return false;
  }
  return true;
}
int main()
{
  int N, K, tmp;
  scanf("%d", &N);
  for(int i=1; i<=N; i++)
  {
    scanf("%d", &tmp);
    ranks[tmp] = i; //存入排名
  }
  scanf("%d", &K);
  fill(checked, checked+maxn, false);
  for(int i=0; i<K; i++)
  {
    scanf("%d", &tmp);
    printf("%04d: ", tmp);
    if(checked[tmp])    //如果已经查过,则打印Checked
    {
      printf("Checked\n");
      continue;
    }
    if(ranks[tmp] == 0) //不存在查询的ID
    {
      printf("Are you kidding?\n");
      continue;
    }
    //对应输出
    if(ranks[tmp] == 1) printf("Mystery Award\n");
    else if(checkPrime(ranks[tmp]))	printf("Minion\n");
    else	printf("Chocolate\n");
    checked[tmp] = true;    //设为已查询
  }
  return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值