题目链接:1116 Come on! Let’s C (20分)
题意
prime Number 素数,给定N个人的Id按照排名依次给出,给出k次查询,求该id获得什么奖励。第一输出 Mystery Award ,素数名次输出Minion,其他输出Chocolate,第二次被询问则输出Checked,没有该ID则输出Are you kidding?
分析
- 数据范围为10000,所以可以使用桶,设置10000个桶,输入时id对应的桶的值为名次。
- 然后在设置个记录数组book,用来记录是否被输出过,初始为0,输入过id的对应的桶为1,被查询过为2.
- 判断10000以内的素数。
- 列写条件判断输出。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */using namespace std;
const int maxn = 10001;
/** prime number 素数 */
int a[maxn] = {
0}, book[maxn] = {
0};
int prime[maxn] = {
0};
void isPrime() {
prime[1] = prime[0] = 1;for (int i = 2; i < maxn; i++) {
for (int j = i + i; j < maxn; j += i)prime[j] = 1;}
}int main(int argc, char** argv) {
int n, k, first, x;scanf("%d", &n);isPrime();for (int i = 1; i <= n; i++) {
scanf("%d", &x);a[x] = i; // 排名 book[x] = 1; // 标记 if (i == 1)first = x;}scanf("%d", &k);for (int i = 0; i < k; i++) {
scanf("%d", &x);if (book[x] == 0) {
printf("%04d: Are you kidding?\n", x);} else if (book[x] > 1) {
printf("%04d: Checked\n", x);} else if (x == first) {
printf("%04d: Mystery Award\n", x);book[x]++;} else if (prime[a[x]] == 0) {
printf("%04d: Minion\n", x);book[x]++;} else {
printf("%04d: Chocolate\n", x);book[x]++;}}return 0;
}